添加2字符变量的ASCII值



我刚刚开始自己学习Java,但是我被困在这里,所以你能给我解释一下这是如何工作的吗?下面是练习:

在这个练习中,Ze会问你 zomtomatoes 你想买的锌

您必须在[A-Za-z]范围内输入一个ASCII字符。你想买的土豆数量会以a的形式给你变量zomatoes,类型为char

您想要购买的Zinions的数量将以a的形式给您变量名为zinions,其类型也是char

必须添加两个字符变量的ASCII值(zomatoes和zinions),并填充名为items的变量,该变量是of输入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"); 
    } 
} 

好像是作业。

     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.
我已经给你提示了。希望您能更正代码中的错误

您忘记将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

相关内容

  • 没有找到相关文章

最新更新