英文:
If statement loop
问题
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int response;
do
{
System.out.println("What problem do you want to do?");
System.out.println("1:Celsius to Fahrenheit");
System.out.println("2:Slope Formula");
System.out.println("3:Averages");
System.out.println("4:Perimeter");
System.out.println("5:Exit\n\n");
response = keyboard.nextInt();
if (response == 1)
{
System.out.println("Degrees Celsius:");
int degrees = keyboard.nextInt();
System.out.println(degrees + " degrees Celsius is " + celsToFahrenheit(degrees) + " degrees Fahrenheit.");
}
else if (response == 2)
{
System.out.println("x1:");
int x1 = keyboard.nextInt();
System.out.println("y1:");
int y1 = keyboard.nextInt();
System.out.println("x2:");
int x2 = keyboard.nextInt();
System.out.println("y2:");
int y2 = keyboard.nextInt();
System.out.println("The slope is " + slopeFormula(x1, y1, x2, y2));
}
else if (response == 3)
{
System.out.println("First number:");
int num1 = keyboard.nextInt();
System.out.println("Second number:");
int num2 = keyboard.nextInt();
System.out.println("The average is " + average(num1, num2));
}
else if (response == 4)
{
System.out.println("Length:");
int length = keyboard.nextInt();
System.out.println("Width:");
int width = keyboard.nextInt();
System.out.println("The perimeter is " + perimeter(length, width));
}
}
while (response != 5);
}
(Note: I've made a couple of minor corrections to the text. The function names like celsToFahrenheit
, slopeFormula
, average
, and perimeter
should correspond to the actual functions you have defined in your program.)
英文:
So I made a program to do different math problems depending on what the user wants, and I don't want the program to end after the user is done. Right now, the loop I have just repeats the same option over and over. The desired effect is to be able to keep choosing which problem you want to do. I am fairly new to coding so I apologize if this is a simple question.
public static void main(String[] args)
{
System.out.println("What problem do you want to do?");
System.out.println("1:Celcius to Farenheit");
System.out.println("2:Slope Formula");
System.out.println("3:Averages");
System.out.println("4:Perimeter");
System.out.println("5:Exit\n\n");
Scanner keyboard = new Scanner(System.in);
int response = keyboard.nextInt();
do
{
if (response == 1)
{
System.out.println("Degrees Celcius:");
int degrees = keyboard.nextInt();
System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
}
else if (response == 2)
{
System.out.println("x1:");
int x1 = keyboard.nextInt();
System.out.println("y1:");
int y1 = keyboard.nextInt();
System.out.println("x2:");
int x2 = keyboard.nextInt();
System.out.println("y2:");
int y2 = keyboard.nextInt();
System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
}
else if (response == 3)
{
System.out.println("First number:");
int num1 = keyboard.nextInt();
System.out.println("Second number:");
int num2 = keyboard.nextInt();
System.out.println("The average is " + average(num1,num2));
}
else if (response == 4)
{
System.out.println("Length:");
int length = keyboard.nextInt();
System.out.println("Width:");
int width = keyboard.nextInt();
System.out.println("The perimeter is " + perimeter(length,width));
}
}
while (response != 5);
答案1
得分: 1
将这部分代码修改如下:
改成这样:
do
{
int response = keyboard.nextInt();
if (response == 1)
如果你将 keyboard.nextInt()
调用移到 do
循环内部,它将会重复询问用户输入。
你之前将这行代码放在循环之前,因此它只会执行一次。
英文:
Change this bit like this:
do
{
int response = keyboard.nextInt();
if (response == 1)
If you move the keyboard.nextInt()
call to inside the do
loop it will now repeatedly ask the user for input.
You had this line of code before the loop so it only processed once.
答案2
得分: 0
在循环外部声明 int response
。在循环内部放置 response = keyboard.nextInt();
。
使用循环外的 int response = keyboard.nextInt()
,你永远不会再次到达它。同时,将选项放在循环内部,以便它们每次都会显示。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int response;
do {
System.out.println("What problem do you want to do?");
System.out.println("1:Celcius to Fahrenheit");
System.out.println("2:Slope Formula");
System.out.println("3:Averages");
System.out.println("4:Perimeter");
System.out.println("5:Exit\n\n");
response = keyboard.nextInt();
if (response == 1) {
System.out.println("Degrees Celsius:");
int degrees = keyboard.nextInt();
System.out.println(degrees + " degrees Celsius is " + celsToFahrenheit(degrees) + " degrees Fahrenheit.");
} else if (response == 2) {
System.out.println("x1:");
int x1 = keyboard.nextInt();
System.out.println("y1:");
int y1 = keyboard.nextInt();
System.out.println("x2:");
int x2 = keyboard.nextInt();
System.out.println("y2:");
int y2 = keyboard.nextInt();
System.out.println("The slope is " + slopeFormula(x1, y1, x2, y2));
} else if (response == 3) {
System.out.println("First number:");
int num1 = keyboard.nextInt();
System.out.println("Second number:");
int num2 = keyboard.nextInt();
System.out.println("The average is " + average(num1, num2));
} else if (response == 4) {
System.out.println("Length:");
int length = keyboard.nextInt();
System.out.println("Width:");
int width = keyboard.nextInt();
System.out.println("The perimeter is " + perimeter(length, width));
}
} while (response != 5);
}
请注意,我在代码中修复了 "Farenheit" 的拼写错误,将其更正为 "Fahrenheit"。
英文:
Declare int response
outside of the loop. Inside the loop place response = keyboard.nextInt();
With int response = keyboard.nextInt()
outside of the loop, you never reach it again. Also, place the options inside the loop so they will display each time.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int response;
do {
System.out.println("What problem do you want to do?");
System.out.println("1:Celcius to Farenheit");
System.out.println("2:Slope Formula");
System.out.println("3:Averages");
System.out.println("4:Perimeter");
System.out.println("5:Exit\n\n");
response = keyboard.nextInt();
if (response == 1) {
System.out.println("Degrees Celcius:");
int degrees = keyboard.nextInt();
System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
} else if (response == 2) {
System.out.println("x1:");
int x1 = keyboard.nextInt();
System.out.println("y1:");
int y1 = keyboard.nextInt();
System.out.println("x2:");
int x2 = keyboard.nextInt();
System.out.println("y2:");
int y2 = keyboard.nextInt();
System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
} else if (response == 3) {
System.out.println("First number:");
int num1 = keyboard.nextInt();
System.out.println("Second number:");
int num2 = keyboard.nextInt();
System.out.println("The average is " + average(num1,num2));
} else if (response == 4) {
System.out.println("Length:");
int length = keyboard.nextInt();
System.out.println("Width:");
int width = keyboard.nextInt();
System.out.println("The perimeter is " + perimeter(length,width));
}
}
while (response != 5);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论