好的,对于这段代码,我要求某人最喜欢的号码,然后让他们挑选一封信。该字母分配给变量。例如,如果他们选择的字母是A(或小写字母A),则将两个加到原始数字,如果字母是m,则数字乘以2,如果字母是S,则数字减去2 。但是,我想保持循环,并且在应用一个字母后,说A,并且数字加上2,我希望该总数通过另一个循环。问题是,我不知道如何让它返回循环,以便它可以继续询问字母,以便它可以继续添加,减去或相乘。
例如,收藏号码是4,我选择字母A并且有2个添加到A,而持有号码4的变量现在保持号码6.现在它再次询问,我应该选择哪个字母,我选择再一次,变量现在是8,我选择m并且它乘以2得到16,依此类推,直到选择了除A,S或M之外的字符。
这是我在网上看到的一个旧java测试的问题。它没有完成结果,我正在努力寻找答案。我是一名初学Java程序员,试图向Youtube学习,而且我大部分时间都没有得到完整的解释,所以请耐心等待代码:/
import java.util.Scanner;
public class question2
{
public static void main(String[] args)
{
int Total;
char Letter;
Scanner keyboard = new Scanner(System.in);
System.out.println("Whats your favorite number");
Total = keyboard.nextInt();
System.out.println("Pick a letter from the following: A, S, M");
Letter = keyboard.next().charAt(0);
while (Letter == 'A'|| Letter == 'a'|| Letter == 's'|| Letter == 'S'||
Letter == 'm'|| Letter == 'M')
{
if(Letter == 'a'|| Letter == 'A')
{
Total = Total + 2;
System.out.println("2 added to your number is "+Total+".");
return;
}
if(Letter == 's'|| Letter == 'S')
{
Total = Total - 2;
System.out.println("2 subtracted from your number is
"+Total+".");
return;
}
if(Letter == 'm'|| Letter == 'M')
{
Total = Total - 2;
System.out.println("2 subtracted from your number is
"+Total+".");
return;
}
}
}
}
答案 0 :(得分:0)
删除所有语句中的return
,并在循环结束时再次放置:
System.out.println("Pick a letter from the following: A, S, M");
Letter = keyboard.next().charAt(0);
答案 1 :(得分:0)
Scanner keyboard = new Scanner(System.in);
while (true){
System.out.println("Whats your favorite number");
Total = keyboard.nextInt();
System.out.println("Pick a letter from the following: A, S, M");
Letter = keyboard.next().charAt(0);
if...
if...
if...
}
并从if语句中删除返回值。在旁注中,用小写字母开始变量名称。
答案 2 :(得分:0)
重构您的代码以使用do while循环:
do {
System.out.println("Pick a letter from the following: A, S, M");
String input = keyboard.next().toUpperCase();
if (input.equals("S") || input.equals("M")) {
Total = Total - 2;
System.out.println("2 subtracted from your number is " + Total + ".");
}
else if (input.equals("A")) {
Total = Total + 2;
System.out.println("2 added to your number is " + Total + ".");
}
} while(input.equals("S") || input.equals("M") || input.equals("A"));
只要用户输入A
,M
或S
,不区分大小写,上述循环就会旋转。
我还更改了扫描仪逻辑以捕获整个字符串,而不是单个字符。使用字符串与字符以不区分大小写的方式处理输入要容易得多。
答案 3 :(得分:0)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
long favNumber = 4;
boolean goOn = true;
do {
char ch = reader.next().charAt(0);
switch (ch) {
case 'A':
case 'a':
favNumber += 2;
break;
case 'S':
case 's':
favNumber -= 2;
break;
case 'M':
case 'm':
favNumber *= 2;
break;
default:
goOn = false;
break;
}
} while (goOn);
System.out.println(favNumber);
}
}
答案 4 :(得分:0)
对代码进行最低限度的更改,现在就是这样。
import java.util.Scanner;
public class question2
{
public static void main(String[] args)
{
int Total;
char Letter;
Scanner keyboard = new Scanner(System.in);
System.out.println("Whats your favorite number");
Total = keyboard.nextInt();
System.out.println("Pick a letter from the following: A, S, M");
Letter = keyboard.next().charAt(0);
while (Letter == 'A'|| Letter == 'a'|| Letter == 's'|| Letter == 'S'||
Letter == 'm'|| Letter == 'M')
{
if(Letter == 'a'|| Letter == 'A')
{
Total = Total + 2;
System.out.println("2 added to your number is "+Total+".");
}
if(Letter == 's'|| Letter == 'S')
{
Total = Total - 2;
System.out.println("2 subtracted from your number is
"+Total+".");
}
if(Letter == 'm'|| Letter == 'M')
{
Total = Total - 2;
System.out.println("2 subtracted from your number is
"+Total+".");
}
Letter = keyboard.next().charAt(0);
}
return;
}
}
在这里,我们不会在完成一个操作后结束函数(return
),相反,我们会询问用户的另一封信并返回检查该字母是否为/ m /的条件s并因此重复该过程,直到用户输入另一个字母。
当然,其他答案有更多记录良好,干净和标准化代码,所以我想用 minimal 更改发布代码。
但请仔细阅读其他答案以获得更好的编程实践。