我是Java新手,我想制作一个程序,通过键入你想要多少甜甜圈来计算甜甜圈的价格,然后根据你想要的甜甜圈数量告诉你价格是多少。
这是代码
import java.util.Scanner;
class MyFirstProgram {
public static void main(String args []){
System.out.println("How many donuts do you want? Type in a number.");
Scanner donuts = new Scanner(System.in);
int price; price = 2;
System.out.print("Price of " + donuts + " donuts is ");
System.out.println(donuts * price);
}
}
我的代码中缺少什么?我在最后一行收到错误。我想我需要将甜甜圈声明为整数,这样我就可以将它与价格相乘。我需要做什么?
编辑:我明白了。这是现在的工作程序: import java.util.Scanner;
class MyFirstProgram {
public static void main(String args []){
System.out.println("Koliko krafna želiš? Upiši broj.");
Scanner krafne = new Scanner(System.in);
int num = krafne.nextInt();
int cijena; cijena = 2;
System.out.print("Cijena " + num + " krafni jest ");
System.out.print(num * cijena);
System.out.println(" kn.");
}
}
我用克罗地亚语写了这个节目。
答案 0 :(得分:1)
甜甜圈是一个Scanner类对象而不是您的输入。 替换你的行
Scanner donuts = new Scanner(System.in);
以下
Scanner sc=new Scanner(System.in);
int donuts=sc.nextInt();
答案 1 :(得分:1)
要获取输入表单控制台,您应该使用Scanner类对象
试试这个
import java.util.Scanner;
class MyFirstProgram { public static void main(String args []){
System.out.println("How many donuts do you want? Type in a number.");
Scanner scr = new Scanner(System.in);
int donuts = scr.nextInt(); /*scr is object of Scanner class and next int is used to take integer input*/
int price; price = 2;
System.out.print("Price of " + donuts + " donuts is ");
System.out.println(donuts * price);
}
}