cocaine4ik
@cocaine4ik

Почему переменная roof не меняет своего значения?

import java.util.Scanner;

public class House {
	
Scanner in = new Scanner(System.in);

int chooseRoof(int roof) {
	for( ; ; ) {
		System.out.println("Choose your roof material:");
		System.out.println("Default roof - 45 \t Tile roof - 95 \t Slate roof - 126");
		System.out.println("Enter a number of the choosen material:");
		String input = in.nextLine();
		
		// checking input, if input is empty, warn user about it
		// and ask to try again, after that - continue loop.
						
		if(input.isEmpty()) {
			System.out.println("You don't enter anything.");
			System.out.println();
			continue;
		}
		
		// checking input, if input have any characters, warn user about it
		// and ask to try again, after that - continue loop.
		
		if(!input.matches("[-+]?\\d+")){
			System.out.println("The number must consists only numerals.");
			System.out.println();
			continue;
		}
		
		if (input.equals("45") || input.equals("95") || input.equals("126")) {
			roof = Integer.parseInt(input);
			return roof;
		}
		else {
			System.out.println("Your enter wrong number. Pls choose the material and "
					+ "use his number.");
			System.out.println();
			continue;
		}
	}
	
}


public class Building {
	public static void main(String args[]) {
        int roof = 45;
        House home = new House();
        home.chooseRoof(roof); // Переменная roof должна поменять свое значение
        System.out.println(roof); // Но когда мы ее выводим, видим старое значение,
                                             // почему? Как это исправить?
        }
}
  • Вопрос задан
  • 223 просмотра
Решения вопроса 1
@D3lphi
Как в java примитивные типы передаются? По значению или по ссылке? По значению, конечно. А это значит, что изменение значения этой переменной в методе chooseRoof() никак не повлияет на значение переменной в методе main().
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы