2010-02-19 35 views
14

我們的教授正在讓我們用Java進行一些基本編程,他給了一個網站和一切註冊並提交我們的問題,今天我需要做這個例子,我覺得我喜歡在正確的軌道上,但我無法弄清楚其餘部分。下面是實際的問題:如何在Java中使用多行閱讀輸入

**Sample Input:** 
10 12 
10 14 
100 200 

**Sample Output:** 
2 
4 
100 

,這裏是什麼,我已經得到了到目前爲止:

public class Practice { 

    public static int calculateAnswer(String a, String b) { 
     return (Integer.parseInt(b) - Integer.parseInt(a)); 
    } 

    public static void main(String[] args) { 
     System.out.println(calculateAnswer(args[0], args[1])); 
    } 
} 

現在,我總是得到的答案2因爲我讀了單行線,我怎麼能考慮所有的行?謝謝

出於某種奇怪的原因,我想執行每一次我得到這個錯誤:

C:\sonic>java Practice.class 10 12 
Exception in thread "main" java.lang.NoClassDefFoundError: Fact 
Caused by: java.lang.ClassNotFoundException: Fact.class 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:20 
     at java.security.AccessController.doPrivileged(Native M 
     at java.net.URLClassLoader.findClass(URLClassLoader.jav 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:307 
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher. 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:248 
Could not find the main class: Practice.class. Program will exit. 

無論答案,我用我得到這個錯誤的版本,我該怎麼辦?

但是,如果我運行它在Eclipse運行方式>運行配置 - >程序參數

10 12 
10 14 
100 200 

我沒有得到任何輸出

編輯

我已經取得了一些進展,在第一我得到編譯錯誤,然後運行時錯誤,現在我得到了錯誤的答案,所以任何人都可以幫助我這是什麼問題:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.math.BigInteger; 

public class Practice { 

    public static BigInteger calculateAnswer(String a, String b) { 
     BigInteger ab = new BigInteger(a); 
     BigInteger bc = new BigInteger(b); 
     return bc.subtract(ab); 
    } 

    public static void main(String[] args) throws IOException { 
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 
     String line; 

     while ((line = stdin.readLine()) != null && line.length()!= 0) { 
      String[] input = line.split(" "); 
      if (input.length == 2) { 
       System.out.println(calculateAnswer(input[0], input[1])); 
      } 
     } 
    } 
} 
+1

難道你的教授指定他如何希望你獲得輸入到你的程序?從命令行讀取文件,在程序運行時輸入到程序中? – Nate 2010-02-19 14:37:23

+0

@Nate umm他給了我們一個網站http://uva.onlinejudge.org,所以我們在那裏註冊給他我們的用戶名,他希望我們每天解決一個問題,這是一件好事..因爲我不能得到這個例子在我的電腦上工作,沒有任何修飾它的目的。我試圖解決的問題是'10055 - Hashmat勇敢的戰士'http://acm.uva.es/p/v100/10055.html – 2010-02-19 14:41:35

回答

15

我終於得到它,submited它拒絕任何理由13次,第14次 「法官」 接受我的答案,那就是:

import java.io.BufferedInputStream; 
import java.util.Scanner; 

public class HashmatWarrior { 

    public static void main(String args[]) { 
     Scanner stdin = new Scanner(new BufferedInputStream(System.in)); 
     while (stdin.hasNext()) { 
      System.out.println(Math.abs(stdin.nextLong() - stdin.nextLong())); 
     } 
    } 
} 
+0

使用掃描儀類是我正在尋找的確切的東西,非常感謝 – 2012-03-29 18:31:11

1

調查BufferedReader。如果這不是一般/高級別,我建議閱讀I/O tutorial

+0

@Hank Gay不是來自文件,它會很容易做到它從控制檯參數 – 2010-02-19 14:00:11

+1

@Gandalf,你可以用'BufferedReader'來做到這一點。 'BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));' – 2010-02-19 14:07:09

8

使用BufferedReader,你可以把它從標準輸入這樣寫着:

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 
String line; 

while ((line = stdin.readLine()) != null && line.length()!= 0) { 
    String[] input = line.split(" "); 
    if (input.length == 2) { 
     System.out.println(calculateAnswer(input[0], input[1])); 
    } 
} 
1

很多學生練習使用Scanner,因爲它有多種方法來解析號碼。我通常只是開始慣用的面向行的過濾器:

import java.io.*; 

public class FilterLine { 

    public static void main(String[] args) throws IOException { 
     BufferedReader in = new BufferedReader(
      new InputStreamReader(System.in)); 
     String s; 

     while ((s = in.readLine()) != null) { 
      System.out.println(s); 
     } 
    } 
} 
0

您在命令行中運行的問題是,你不把「的.class」類文件之後。

java Practice 10 12

應該工作 - 只要你是什麼地方的java可以找到.class文件。

類路徑問題是一個完整的故事。如果java仍然抱怨說它找不到你的類,請轉到與你的.class文件相同的目錄(並且它沒有出現你正在使用的包...),並嘗試 -

java -cp . Practice 10 12

+0

@Nate我仍然得到相同的錯誤信息 – 2010-02-19 14:47:22

+0

是在執行'java -cp的相同目錄下的Practice.class。練習10 12'來自? – Nate 2010-02-19 16:20:20

-1
import java.util.*; 
import java.io.*; 

public class Main { 
    public static void main(String arg[])throws IOException{ 
     InputStreamReader isr = new InputStreamReader(System.in); 
     BufferedReader br = new BufferedReader(isr); 
     StringTokenizer st; 
     String entrada = ""; 

     long x=0, y=0; 

     while((entrada = br.readLine())!=null){ 
      st = new StringTokenizer(entrada," "); 

      while(st.hasMoreTokens()){ 
       x = Long.parseLong(st.nextToken()); 
       y = Long.parseLong(st.nextToken()); 
      } 

      System.out.println(x>y ?(x-y)+"":(y-x)+""); 
     } 
    } 
} 

這個解決方案比上面的解決方案更高效,因爲它佔用了2.128,而這需要1.308秒才能解決問題。

+0

由於這是一個教育活動,沒有必要做性能改進。代碼可讀性>性能! – Kuchi 2015-10-11 22:50:27

-2
package pac001; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

public class Entry_box{ 



    public static final String[] relationship = {"Marrid", "Unmarried"}; 


    public static void main(String[] args) 
    { 
     //TAKING USER ID NUMBER 
      int a = Integer.parseInt(JOptionPane.showInputDialog("Enter ID no: ")); 
     // TAKING INPUT FOR RELATIONSHIP 
     JFrame frame = new JFrame("Input Dialog Example #3"); 
     String Relationship = (String) JOptionPane.showInputDialog(frame,"Select Your Relationship","Married", 
           JOptionPane.QUESTION_MESSAGE, null, relationship,relationship[0]); 



     //PRINTING THE ID NUMBER 
     System.out.println("ID no: "+a); 
     // PRINTING RESULT FOR RELATIONSHIP INPUT 
      System.out.printf("Mariitual Status: %s\n", Relationship); 

     } 
} 
1
public class Sol { 

    public static void main(String[] args) { 

    Scanner sc = new Scanner(System.in); 

    while(sc.hasNextLine()){ 

    System.out.println(sc.nextLine()); 

    } 
} 
} 
+1

如果對您的代碼的解釋會很好,請添加一點。 – Sakuto 2017-05-03 08:25:21