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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名