Answer the question
In order to leave comments, you need to log in
How to subtract from a number in Java so that the result is preserved and subtract the next number from this result?
There is a water tank, for example 1000 ml, and there are 2 cups: the first is 100 ml, the second is 200 ml. Water is scooped out of the tank in different sequences by these cups (at random, without any system: first, second, second, first, etc.), when the tank is empty, the user sees a message that the water has run out. I did the "scooping out" of water through the scanner and if / else (if the user enters the console 1, then the first mug, if 2, then the second). And how to make it so that one common value (a tank of 1000 ml) after scooping out 100 ml, returns a new value and the next mug would be subtracted from the new value? The for loop displays all the values at once and is not quite suitable because there are two circles, return stops the program. Maybe there are just some solutions, but I just don't know about them?
Answer the question
In order to leave comments, you need to log in
And how to make it so that one common value (a tank of 1000 ml) after scooping out 100 ml, returns a new value and the next mug would be subtracted from the new value?
public class Main {
public static void main(String[] args) {
int volume = 1000;
Scanner scanner = new Scanner(System.in);
System.out.println(
"Объем бака равен - 1000 мл. \n" +
"Введите значение 100 мл. или 200 мл."
);
while (volume >= 0) {
int input = scanner.nextInt();
if(input > volume) {
System.out.println("В баке нет такого объема воды!");
continue;
} else if(input == 100) {
volume -= input;
System.out.println("Остаток в баке: " + volume);
} else if (input == 200) {
volume -= input;
System.out.println("Остаток в баке: " + volume);
} else {
System.out.println("Вы ввели недействительное значение!");
}
if(volume == 0) {
System.out.println("Вода в баке закончилась");
break;
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question