初学者求问!!! 求大佬解答
package test;import java.util.Scanner;
class Test1 {
static double Toothbrush = 8.8;
static double Towel = 10.0;
static double Water_cup = 18.8;
static double Apple = 12.5;
static double Banana = 15.5;
public static void main(String[] args) {
System.out.println("请输入您要购买的商品:");
Scanner goods = new Scanner(System.in); //这句
Scanner flag = new Scanner(System.in); //这句
String flag1; //这句是啥意思
int goods1; //这里的整型定义的是啥
double sum;
sum=0;
while(true)
{
goods1=goods.nextInt(); //这几句什么意思能帮忙解释一下吗
switch(goods1)
{
case 1:
sum=sum+Toothbrush;
break;
case 2:
sum=sum+Towel;
break;
case 3:
sum=sum+Water_cup;
break;
case 4:
sum=sum+Apple;
break;
case 5:
sum=sum+Banana;
break;
}
System.out.println("需要继续购物请按‘Y’,不需要则‘N’进行结算");
flag1=flag.next();
if(flag1.equals("N")) {
System.out.println("您一共买了"+sum+"块钱");
break;
}
if(flag1.equals("Y")) {
System.out.println("请继续输入要购买商品");
}
}
}
} Scanner goods = new Scanner(System.in); //定义goods为输入对象,建议把对象名改为input或sc,作用是可以调用各种输入方法,就是java里面的cin啦
Scanner flag = new Scanner(System.in); //定义flag为输入对象,和上面一样
String flag1; //定义字符串对象flag1,哪里搞来的代码,命名怪怪的
int goods1; //这里的整型定义的就是整型变量goods1
double sum;
sum=0;
while(true)
{
goods1=goods.nextInt(); //对象goods调用方法nextInt(),输入并赋值给变量goods。方法nextInt(),用于输入整型数据。 慕道子 发表于 2021-9-28 22:30
Scanner goods = new Scanner(System.in); //定义goods为输入对象,建议把对象名改 ...
或许说创建对象比较合适
页:
[1]