我刚刚开始自学Java,但我坚持这个,所以你能解释一下这是如何工作的吗?这是练习:
在本练习中,Ze将询问您 Zomatoes 的数量 Zinions 你想买。
您必须在
[A-Za-z]
范围内输入ASCII字符。 您想要购买的Zomatoes数量将在一个 名为zomatoes
的变量,类型为char
。您要购买的
Zinions
的数量将以a。的形式提供给您 名为zinions
的变量,其类型为char
。您必须添加两个char变量的ASCII值(zomatoes 和zinions)并填充名为
items
的变量 输入int
,加上总和。例如,如果您要求
a
Zomatoes和C
Zinions,则总数 项目数为a(97) + C(67)
= 164
这是他们给我的代码:
public class CrazyConverter {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Hello I am your friendly grocer Darth: ");
System.out.println("How many zomatoes do you want ? (Enter a character in the range [A-Za-z]): ");
String sZomatoes = scanner.nextLine();
System.out.println("How many zinions do you want ? (Enter a character in the range [A-Za-z]): ");
String sZinions = scanner.nextLine();
char zomatoes = sZomatoes.charAt(0);
char zinions = sZinions.charAt(0);
int items = 0;
sZomatoes= 65;
///{
char zinions=(char) asciiValue;
//start your coding here
//end
///}
System.out.println("Thank you ! you have asked for " + items + " items");
}
}
答案 0 :(得分:0)
好像是作业。
char zomatoes = sZomatoes.charAt(0);//storing character for zomatoes
char zinions = sZinions.charAt(0); //storing character for zinions
int items = zomatoes+zinions;//Adding the ascii value of both zomatoes and zinions.
我已经给你提示了。希望你能纠正代码中的错误。
答案 1 :(得分:0)
您忘了将char
(s)加在一起并初始化items
。我想你想要的东西,
char zomatoes = sZomatoes.charAt(0);
char zinions = sZinions.charAt(0);
int items = zomatoes + zinions;
System.out.printf("The total number of items is '%s'(%d) + '%s'(%d) = %d%n",
zomatoes, (int) zomatoes, zinions, (int) zinions, items);
当我用你的样本输入
运行它时Hello I am your friendly grocer Darth:
How many zomatoes do you want ? (Enter a character in the range [A-Za-z]):
a
How many zinions do you want ? (Enter a character in the range [A-Za-z]):
C
The total number of items is 'a'(97) + 'C'(67) = 164