#java #loops
#java #циклы
Вопрос:
итак, у нас есть задание о счетчике возврата. Я хочу вернуться к процессу, если вы ввели сумму, которой недостаточно для вашей цены. Кажется, я не могу этого сделать, потому что, когда я пытаюсь выполнить цикл «do», отсканированные данные не будут считаны «while ()». Вот и все, спасибо.
это весь код, процесс, с которого начинается моя проблема, находится в «// сумма наличных покупателей»
package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//choosing of product
System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
System.out.println("nEnter 1 for Milk");
//scanners and booleans for product type
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
//if statements for milk
if(milk){
System.out.println("nThis will cost P15 each, how much would you like to buy?");
//scanners and booleans for product price
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
int price = 15 * amount;
System.out.println("nYour product costs P" price);
//amount of buyers cash
System.out.println("nPlease enter how much will you pay");
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//if statement for not enough cash
if(cash < price){
System.out.println("Sorry, your cash is not enough. Please enter amount again");
// this is the part where I want to go back to "amount of buyers cash"
}
if(cash >= price){
int change = cash - price;
System.out.println("nYour change is P" change);
System.out.println("nThank you! Come again next time.");
System.exit(0);
}
}
}}
Комментарии:
1. Я нигде не вижу цикла выполнения. Можете ли вы показать нам фактический код, который не работает должным образом? И можете ли вы объяснить, каким образом это не работает? Не удается ли скомпилировать? Есть ли исключение? Неожиданный результат? Вообще нет вывода?
Ответ №1:
Я думаю, вам просто нужно изменить условие if на условие while при проверке, превышает ли цена наличные.
package prelim.activities;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//choosing of product
System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
System.out.println("nEnter 1 for Milk");
//scanners and booleans for product type
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
//if statements for milk
if(milk){
System.out.println("nThis will cost P15 each, how much would you like to buy?");
//scanners and booleans for product price
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
int price = 15 * amount;
System.out.println("nYour product costs P" price);
//amount of buyers cash
System.out.println("nPlease enter how much will you pay");
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//if statement for not enough cash
while(cash < price){
System.out.println("Sorry, your cash is not enough. Please enter amount again");
// this is the part where I want to go back to "amount of buyers cash"
Scanner money1 = new Scanner(System.in);
cash = money1.nextInt();
}
if(cash >= price){
int change = cash - price;
System.out.println("nYour change is P" change);
System.out.println("nThank you! Come again next time.");
System.exit(0);
}
}
}}
Ответ №2:
Если я правильно понимаю, вы хотите обновлять цену каждый раз, для этого вам нужно изменить область действия цены
package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
public static void main(String[] args) {
//choosing of product
System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
System.out.println("nEnter 1 for Milk");
//scanners and booleans for product type
Scanner scanner = new Scanner(System.in);
String product = scanner.nextLine();
boolean milk = true;
// price moved here so it will be accessible through out the method.
int price = 0;
//if statements for milk
if(milk){
System.out.println("nThis will cost P15 each, how much would you like to buy?");
//scanners and booleans for product price
Scanner scanner_price = new Scanner(System.in);
int amount = scanner_price.nextInt();
price = 15 * amount;
System.out.println("nYour product costs P" price);
//amount of buyers cash
System.out.println("nPlease enter how much will you pay");
Scanner money = new Scanner(System.in);
int cash = money.nextInt();
//if statement for not enough cash
if(cash < price){
System.out.println("Sorry, your cash is not enough. Please enter amount again");
// this is the part where I want to go back to "amount of buyers cash"
}
if(cash >= price){
int change = cash - price;
System.out.println("nYour change is P" change);
System.out.println("nThank you! Come again next time.");
System.exit(0);
}
}
}}