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条)

报告相同问题?

悬赏问题

  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件