Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

java - 我坚持使用Java(I am stuck with Java)

1: Hi there.

(1:嗨)

I don't know why my Java Cannot run.

(我不知道为什么我的Java无法运行。)

I think that my code is fine.

(我认为我的代码很好。)

Can anyone help me out?

(谁能帮我吗?)

This was the error message that was displayed after I run.

(这是我运行后显示的错误消息。)

The error message is displayed below.

(错误消息显示在下面。)
It would be nice if someone can help me with this code.

(如果有人可以帮助我提供此代码,那就太好了。)

Thank you

(谢谢)

This is the content of my file

(这是我文件的内容)

A   1   50      3
B   2   1300    104
C   3   9000    900
D   4   1500    

where

(哪里)

  • A - D Represents the transaction code

    (A-D代表交易代码)

  • 1 - 4 represents the Employeenumber

    (1-4代表员工编号)

  • 50, 1300,9000,1500 represents the retail price

    (50,1300,9000,1500代表零售价)

and the last section represents the commission that I have calculated

(最后一部分代表我计算的佣金)

  1. This is the Question.

    (这是问题。)

A transaction record on a sales commission file contains the retail price of an item sold, a transaction code which indicates the sales commission category to which an item can belong, and the employee number of the person who sold the item.

(销售佣金文件上的交易记录包含所售商品的零售价格,指示该商品可以所属的销售佣金类别的交易代码以及出售该商品的人的员工编号。)

The transaction code can contain the values A, B or C which indicate that the percentage commission will be 6%, 8% or 10% respectively.

(交易代码可以包含值A,B或C,这表示佣金百分比分别为6%,8%或10%。)

Construct an algorithm that will read a record on the file, calculate the commission owing for that record and print the retail price, commission and employee number.

(构造一个算法,该算法将读取文件上的记录,计算该记录的佣金并打印零售价,佣金和员工编号。)

Convert this to Java

(将此转换为Java)


import java.util.Scanner;
import java.io.*;`

public class Question1
{

    public static void main(String[] args) throws Exception
    {
        Scanner input = new Scanner (new File ("C:\Users\leeli\Desktop\Assignment2Qns1.txt"));
        double retailprice, commission;
        String Empnum;
        char transcode;

        while(input.hasNext())
    {
       retailprice = input.nextDouble();
       commission = input.nextDouble();``
       Empnum = input.next();
       transcode = input.next().charAt(0);

       if (transcode == 'A' || transcode == 'a')
       {
           commission = retailprice *0.06;
       }
       else if (transcode == 'B' || transcode == 'b')
        {
            commission = retailprice *0.08;
        }
       else if (transcode == 'C' || transcode == 'c')
       {
           commission = retailprice *0.1;
       }
       else 
       {
           commission = 0;
           System.out.print("Invalid transaction code");
       }
       System.out.println("Transaction code  Employee Number  Retail Price  Commission");
       System.out.println(transcode + "" + Empnum +"" + retailprice +"" + commission);
}
}
}

//run:
//Exception in thread "main" java.util.InputMismatchException
    //at java.util.Scanner.throwFor(Scanner.java:909)
    //at java.util.Scanner.next(Scanner.java:1530)
    //at java.util.Scanner.nextDouble(Scanner.java:2456)
    //at Question1.main(Question1.java:18)
//Java Result: 1
//BUILD SUCCESSFUL (total time: 0 seconds)

  ask by Vincedeveloper24 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your order of reading from file matters a lot, you were getting the error because you were not reading the content in the correct order.

(您从文件中读取的顺序非常重要,因为您没有以正确的顺序读取内容,所以出现了错误。)

You should read content in this order transcode, Empnum, retailprice, commission.

(您应该阅读此订单中的内容转码,Empnum,retailprice,佣金。)

This will fix your problem :

(这将解决您的问题:)

            transcode = input.next().charAt(0);
            Empnum = input.next();
            retailprice = input.nextDouble();
            commission = input.nextDouble();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...