mayeye1989 2015-09-22 05:08 采纳率: 54.5%
浏览 1822
已采纳

JAVA课程编程作业疑问

老师要求的用户界面显示结果如下:
This program computes the total cost of a purchase. Enter the price of the purchased item. 12.50
Enter how many items are purchased? 3
The final cost of the purchase with the 7% tax applied is $40.125
以下是我的代码:
package jave.util;
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
System.out.println("Please input the number");
Scanner N= new Scanner(System.in);
System.out.println("Please input the price");
Scanner P= new Scanner(System.in);
int number=N.nextInt();
double price=P.nextDouble();
double cost= (price*number*1.07);
System.out.println("The final cost of the purchase with the 7% tax applied is " + cost);
// TODO Auto-generated method stub

}

}

此时我输入数值的时候,两个数值是连续输入的,请问怎么编写才能令输入一个数值之后,再提示输入另外一个?(另外代码有什么可以优化的地方也请知道一下)

  • 写回答

3条回答 默认 最新

  • 心随自在飞 2015-09-22 08:56
    关注

    调整后大致如下,不需要两个Scanner 对象,也不要太多的变量声明
    package jave.util;
    import java.util.Scanner;
    public class Assignment1 {
    public static void main(String[] args) {
    Scanner N= new Scanner(System.in);
    System.out.println("Please input the number");
    int number=N.nextInt();
    System.out.println("Please input the price");
    double price=N.nextDouble();
    System.out.println("The final cost of the purchase with the 7% tax applied is $" +(price*number*1.07));
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?