我已经做了一些Java编码已经有一段时间,但这是一个简单的程序,我做了计算我的零售商店的标记。在我自己关闭之前,我怎样才能使程序循环?现在,我在我的终端/命令提示符下运行这个程序,每次我输入一个它结束的值,所以我被迫再次执行它需要一些时间(约.05秒或更少)但是我需要所有的时间我我可以完成这项任务。所以重点是让它保持运行直到我自己关闭它。
import static java.lang.System.out;
import java.util.Scanner;
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
}
}
}
答案 0 :(得分:1)
在Java中,您可以使用以下三个循环之一:
while循环是一种控制结构,允许您重复任务一定次数。
语法: while循环的语法是:
while(Boolean_expression)
{
//Statements
}
执行时,如果boolean_expression结果为真,则执行循环内的动作。这将持续很长时间 因为表达式结果是真的。
语法: do ... while循环的语法是:
do
{
//Statements
}while(Boolean_expression);
请注意,布尔表达式出现在循环的末尾,所以 循环中的语句在测试布尔值之前执行一次。
如果布尔表达式为真,则控制流跳回来执行,循环中的语句再次执行。此过程 重复,直到布尔表达式为假。
for循环是一种允许你的重复控制结构 有效地编写一个需要执行特定数量的循环 次。
当您知道任务的次数时,for循环很有用 重复。
语法: for循环的语法是:
for(initialization; Boolean_expression; update)
{
//Statements
}
在for循环的情况下,如果你想永远重复你的过程,你可以使用如下。
for(;;){
whatever process you have
}
希望从我提供的内容中解决您的问题。
答案 1 :(得分:0)
正如Phylogenesis所说,你可以简单地将代码包装在while(true)
中。但是,如果您想询问是否应该重新启动程序,请使用
Scanner myScanner = new Scanner(System.in);
boolean running = true;
while(running)
{
// your program code here
System.out.print("restart? ");
running = myScanner.nextLine().trim().equalsIgnoreCase("yes");
}
答案 2 :(得分:0)
使用带有sentinel值的while循环来连续获取输入。对于此示例,当您完成输入-1退出时。
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
//while loop here
while(quantity != -1){
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
out.print("What is the quantity?");
int quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
}
}
}
答案 3 :(得分:0)
import static java.lang.System.out;
import java.util.Scanner;
public class ProductMarkup {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner(System.in);
int quantity;
do {
out.print("What is the quantity?");
quantity = myScanner.nextInt();
out.print("What is the wholesale price?");
double wsprice = myScanner.nextDouble();
if (quantity == 1) {
double math = (int) (wsprice * .70);
double retail = math + wsprice;
out.println(retail + 5);
} else if (quantity == 2) {
double math = wsprice * 2;
double retail = math + 10;
out.println(retail);
} else if (quantity == 3) {
double math = wsprice * 2.5;
double retail = math + 5.75;
out.println(retail);
} else if (quantity == 4) {
double math = wsprice * 2.80;
double retail = math + 3.75;
out.println(retail);
}
} while (quantity != -1);
}
}