2014-09-19 47 views
-1

我只想問我如何讓我的代碼只獲取輸入而不是聲明它?這是我的程序。我想輸入不同的原子序數,而不僅僅是我的代碼中的「37」。不介意我的意見,這是用我的母語。謝謝!如何讓我的代碼接受輸入

public class ElectConfi {  

     public static void main(String s[]) { 

      int atomicNumber = 37; 
      String electronConfiguration = getElectronConfiguration(atomicNumber); 
      System.out.println(electronConfiguration); 
     } 
     public static String getElectronConfiguration(int atomicNumber) { 

      int[] config = new int[20]; //dito nag store ng number of elec. in each of the 20  
      orbitals. 
      String[] orbitals = {"1s^", "2s^", "2p^", "3s^", "3p^", "4s^", "3d^", "4p^", "5s^", 
      "4d^", "5p^", "6s^", "4f^", "5d^", "6p^", "7s^", "5f^", "6d^", "7p^", "8s^"}; 
      //Names of the orbitals 
      String result=""; 
      for(int i=0;i<20;i++) //dito ung i represents the orbital and tapos ung j 
      represents ng electrons 
      { 
        for(int j=0;(getMax(i)>j)&&(atomicNumber>0);j++,atomicNumber--) //if atomic 
        number > 0 and ung orbital ay kaya pa magsupport ng more electrons, add 
        electron to orbital ie increment configuration by 1 
        { 
          config[i]+=1; 
        } 
        if(config[i]!=0)  //d2 nagche-check to prevent it printing empty 
        orbitals 
        result+=orbitals[i]+config[i]+" ";  //orbital name and configuration 
        correspond to each other 
      } 
      return result; 
     } 
     public static int getMax(int x) //returns the number of max. supported electrons by each 
     orbital. for eg. x=0 ie 1s supports 2 electrons 
     { 
      if(x==0||x==1||x==3||x==5||x==8||x==11||x==15||x==19) 
        return 2; 
      else if(x==2||x==4||x==7||x==10||x==14||x==18) 
        return 6; 
      else if(x==6||x==9||x==13||x==17) 
        return 10; 
      else 
        return 14; 
     } 
} 
+0

通過使用Scanner來測試? – Synoon 2014-09-19 06:42:02

回答

2

您可以使用一個ScannerBufferedReader並得到用戶輸入

使用Scanner

Scanner scanner = new Scanner(System.in); 
System.out.println("Please input atomic number"); 
int atomicNumber = scanner.nextInt(); 

使用BufferedReader

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
int atomicNumber = Integer.parseInt(reader.readLine()); 
+0

做到了這一點,但得到的錯誤。這是我最新的代碼。 http://pastebin.com/g55d7Pp1 – user3604330 2014-09-19 07:27:16

+0

你得到的錯誤是什麼? – 2014-09-19 08:03:42

0
public static String getElectronConfiguration(int atomicNumber) {} 

此方法接受任何int值並將返回String結果。所以你只需要提供不同的數字作爲輸入。這種方法不需要改變。

如何提供不同的輸入?

您可以使用Scanner來做到這一點。

Scanner scanner = new Scanner(System.in); 
System.out.println("Please input atomic number"); 
int atomicNumber = scanner.nextInt(); 

現在打電話給你的方法

String electronConfiguration = getElectronConfiguration(atomicNumber); 

什麼別的辦法嗎?

您可以定義的一組值在你的代碼atomicNumber,你可以運行那些在一個循環

+0

做到了這一點,但得到的錯誤。這是我最新的代碼。 pastebin.com/g55d7Pp1 – user3604330 2014-09-19 07:27:48

+0

@ user3604330檢查此。 http://ideone.com/J7GhC9,相同的代碼。它沒有給出任何錯誤。 – 2014-09-19 07:36:05

0

你可以從一個命令行參數的用戶輸入:

public static void main(String s[]) { 

     if (s.length == 0) { 
      // Print usage instructions 
     } else { 
      int atomicNumber = Integer.parseInt(s[0]); 
      // rest of program 
     } 
    } 
0

你可以輸入從下面做的命令行參數:

Scanner scanner = new Scanner(System.in); 
String inputLine = scanner.nextLine(); //get entire line 
//or 
int inputInt= scanner.nextInt();//get an integer 

檢查java.util.Scaner API的更多信息 - http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

希望這有助於!