我正在循环中处理Switch语句。我想添加名称并为这些名称指定字母,然后根据字母打印出总值。问题是它不会读取字母的指定值,您可以在下面的代码中看到。例如,对于迈克,我指定了A,而A的值为10.对于Joe,我指定了B,而B的值为7.所以我需要A + B的总值乘以固定值3是51.我肯定做错了什么,并想知道是否可以在不使用对象和其他方法的情况下做到这一点。
import java.util.Scanner;
public class TotalScore {
public static void main(String[] args) {
double lettervalue = 0,fixedvalue=3,totalvalue;
int count,repeatcount;
String valuename = "";
String letter="";
Scanner input = new Scanner(System.in);
System.out.print("How many values do you want to process? ");
count=input.nextInt();
input.nextLine();
for(repeatcount=0;repeatcount<count;++repeatcount){
System.out.print("Enter name for value " +(repeatcount+1)+" ");
valuename = valuename+input.nextLine()+"\n";
System.out.print("Enter letter A,B,C or D for named value ");
letter = letter+input.nextLine()+"\n";
switch(letter){
case "A":case"a": lettervalue=10.0;
break;
case "B":case"b": lettervalue=7.0;
break;
case "C":case"c": lettervalue=5.0;
break;
case "D":case"d": lettervalue=2.0;
break;
default: lettervalue=0;
}
}
totalvalue=lettervalue*fixedvalue;
System.out.println("The value names are\n "+valuename);
System.out.println("The letter values you entered are\n "+letter);
System.out.println("The total value is "+totalvalue);
}
}
Output:
How many values do you want to process? 2
Enter name for value 1 Mike
Enter letter A,B,C or D for named value A
Enter name for value 2 Joe
Enter letter A,B,C or D for named value B
The value names are
Mike
Joe
The letter values you entered are
A
B
The total value is 0.0
答案 0 :(得分:1)
letter
应该只包含您的输入值。然后你可以有一个单独的字符串来跟踪你的总输入。
String inputs = "";
String letter = "";
/*for loop etc.*/
letter = input.nextLine();
/*switch statement*/
inputs += valuename + letter + lettervalue + "\n"; //String.valueOf() is not necessary
totalvalue += lettervalue * fixedvalue;
/*end loop*/
System.out.println(inputs);
答案 1 :(得分:0)
答案应该是这样的。
import java.util.Scanner;
public class TotalScore {
public static void main(String[] args) {
double lettervalue = 0,fixedvalue=3,totalvalue = 0.0;
int count,repeatcount;
String valuename = "";
String letter="";
String letters="";
Scanner input = new Scanner(System.in);
System.out.print("How many values do you want to process? ");
count=input.nextInt();
input.nextLine();
for(repeatcount=0;repeatcount<count;++repeatcount){
System.out.print("Enter name for value " +(repeatcount+1)+" ");
valuename = valuename+input.nextLine()+"\n";
System.out.print("Enter letter A,B,C or D for named value ");
letter = input.nextLine();
letters = letters+letter;
switch(letter){
case "A":case"a": lettervalue=10.0;
break;
case "B":case"b": lettervalue=7.0;
break;
case "C":case"c": lettervalue=5.0;
break;
case "D":case"d": lettervalue=2.0;
break;
default: lettervalue=0;
}
totalvalue=totalvalue + lettervalue*fixedvalue;
}
System.out.println("The value names are\n "+valuename);
System.out.println("The letter values you entered are\n "+letters);
System.out.println("The total value is "+totalvalue);
}
}
这应该是你要找的。 p>
必须进行以下更改
1
totalvalue=totalvalue + lettervalue*fixedvalue;
2
moved the totalValue within the for loop
3
created a new variable letters to be the set of all the letters read.