2

私は現在、Javaで小さなタスクを行っていますが、これは非常に初めてなので、私が犯したばかげた間違いを許してください。基本的に、テキスト ドキュメントから 2 つの値を取得し、それらを Java ドキュメントにインポートしてから乗算しようとしています。これらの 2 つの数値は、時給と労働時間を表すためのものであり、出力はスタッフのメンバーが稼いだ合計金額です。これは私がこれまでに持っているものです...

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 に答える