2

我目前正在用java做一个小任务,我很陌生,所以请原谅我犯的任何愚蠢的错误。基本上我正在尝试从文本文档中获取 2 个值,将它们导入到我的 java 文档中,然后将它们相乘。这两个数字表示小时工资和工作小时数,然后输出是员工的总收入。这是我到目前为止...

import java.util.*;
import java.io.*;  

public class WorkProject 
{ 

    Scanner inFile = new Scanner(new FileReader("staffnumbers.txt"));

    double Hours;
    double Pay;

    Hours = inFile.nextDouble();
    Pay = inFile.nextDouble();
    double earned = Length * Width;

            System.out.println(earned);
    }

到目前为止,我基本上是在尝试将 .txt 文档放入我的 java 文件中。我不确定这是否正确,然后我不确定该去哪里获取要相乘的值并将其输出。我知道到目前为止我所拥有的可能只是我需要的一切的开始,但是由于我渴望学习,任何帮助都将受到极大的赞赏。非常感谢....汉娜

4

2 回答 2

3

我不知道是什么Amount earned。所以我的猜测是你需要将最后一行更改为

double amountEarned = Hours * Pay; //this multiplies the values
System.out.println(amountEarned);  //this outputs the value to the console

编辑:将代码放入main方法中:

public class WorkProject {
    public static void main(String[] args) throws FileNotFoundException {

      Scanner inFile = new Scanner(new FileReader("C:\\staffnumbers.txt"));

      double Hours;
      double Pay;

      Hours = inFile.nextDouble();
      Pay = inFile.nextDouble();
      double amountEarned = Hours * Pay;

      System.out.println(amountEarned);
    }
}
于 2013-10-28T18:02:17.110 回答
0
// Matt Stillwell
// April 12th 2016
// File must be placed in root of the project folder for this example 

import java.io.File;
import java.util.Scanner;

public class Input
{

    public static void main(String[] args)
    {

        // declarations
        Scanner ifsInput;
        String sFile;

        // initializations
        ifsInput = null;
        sFile = "";

        // attempts to create scanner for file
        try
        {
            ifsInput = new Scanner(new File("document.txt"));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File Doesnt Exist");
            return;
        }

        // goes line by line and concatenates the elements of the file into a string
        while(ifsInput.hasNextLine())
            sFile = sFile + ifsInput.nextLine() + "\n";     

        // prints to console
        System.out.println(sFile);

    }
}
于 2016-04-12T22:57:01.520 回答