2013-10-19 35 views
1

Herro,嗯我不確定這是你如何使用這個站點,但是呃讓我們得到它...所以我需要這個項目的幫助我不得不這樣做 -如何找到最大的素數,除1之外的最小的因子,數字之和

輸入[] []輸出

A - F - >乘以3,除以4

ģ - J - >除以3 + 25

K - N - >查找最大因子* 2

Ø - Q - >查找最大素包* 3

的R - W - >查找最小因子除1

X - z - >總和數字

所以我在想,如果我的第一對夫婦是正確的,並且需要空間幫助。所以這個Letter代表的是「Z」中最後一個字母的數字,所以它的26,而「A」是第一個,因此它是1.所以如果有迴應...謝謝!包樂;

import java.util.Scanner; 

public class Fun { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     run(); 
    } 

    public static void run() 
    { 
     input(); 
     evaluateAlphabet(); 
     evaluate(); 

    } 
    public static void input() 
    { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter a Letter, please"); 
     x = sc.next(); 
    } 
    public static String x = " "; 
    public static int temp = 0; 
    public static int answer = 0; 

    public static void evaluateAlphabet() 
    { 
     if(x.equals("A")) 
     { 
      temp = 1; 
     } 
     else if (x.equals("B")) 
     { 
      temp = 2; 
     } 
     else if (x.equals("C")) 
     { 
      temp = 3; 
     } 
     else if (x.equals("D")) 
     { 
      temp = 4; 
     } 
     else if (x.equals("E")) 
     { 
      temp = 5; 
     } 
     else if (x.equals("F")) 
     { 
      temp = 6; 
     } 
     else if (x.equals("G")) 
     { 
      temp = 7; 
     } 
     else if (x.equals("H")) 
     { 
      temp = 8; 
     } 
     else if (x.equals("I")) 
     { 
      temp = 9; 
     } 
     else if (x.equals("J")) 
     { 
      temp = 10; 
     } 
     else if (x.equals("K")) 
     { 
      temp = 11; 
     } 
     else if (x.equals("L")) 
     { 
      temp = 12; 
     } 
     else if (x.equals("M")) 
     { 
      temp = 13; 
     } 
     else if (x.equals("N")) 
     { 
      temp = 14; 
     } 
     else if (x.equals("O")) 
     { 
      temp = 15; 
     } 
     else if (x.equals("P")) 
     { 
      temp = 16; 
     } 
     else if (x.equals("Q")) 
     { 
      temp = 17; 
     } 
     else if (x.equals("R")) 
     { 
      temp = 18; 
     } 
     else if (x.equals("S")) 
     { 
      temp = 19; 
     } 
     else if (x.equals("T")) 
     { 
      temp = 20; 
     } 
     else if (x.equals("U")) 
     { 
      temp = 21; 
     } 
     else if (x.equals("V")) 
     { 
      temp = 22; 
     } 
     else if (x.equals("W")) 
     { 
      temp = 23; 
     } 
     else if (x.equals("X")) 
     { 
      temp = 24; 
     } 
     else if (x.equals("Y")) 
     { 
      temp = 25; 
     } 
     else if (x.equals("Z")) 
     { 
      temp = 26; 
     } 
     else if (x.equals("Qwerty")) 
     { 
      temp = 27; 
     } 
    } 
    public static void evaluate() 
    { 
     if(temp>=1 && temp<= 6) 
     { 
      answer = (temp * 3)/4; 
      System.out.println("Answer is " + answer); 
     } 
     else if(temp >= 7 && temp<= 10) 
     { 
      answer = (temp/3) + 25; 
      System.out.println("Answer is " + answer); 
     } 
     else if(temp >= 11 && temp<= 14) 
     { 

     } 

     else if(temp>=15 && temp<= 17) 
     { 
      for(int i = temp; i>0; i--) 
      { 
       for(int j = 2; j <=i/2 + 1; j++) 
       { 
        if(i%j==0) 
        { 
         break; 
        } 
        if(j==i/2 + 1) 
        { 
        answer = i * 3; 
        }   
       } 
      } 
      System.out.println("Answer is " + answer); 
     } 
     else if(temp>=18 && temp<= 23) 
     { 
      answer = temp; 
     } 
     else if(temp>= 24 && temp<=26) 

      answer = (answer * 12)%26; 
      System.out.println("Answer is " + answer); 
      } 
    } 

-Corruption

回答

0

這裏是焦炭爲int轉換一個更好的方法: 假設串具有至少1個字符(檢查它的長度),您可以通過執行獲得溫度:

temp = x.getCharAt(0) - 'A' + 1; 

或者,安全第一:

temp = 0; 
if (x.matches("^[A-Z]{1}$") { 
    temp = x.getCharAt(0) - 'A' + 1; 
} 

發生了什麼事她è?每個字符都有一個ASCII碼,一個整數。所以,當你有2個字符並且你試圖獲得他們之間的距離時,結果是一個int。 'A' - 'A' = 0(這就是爲什麼我加了一個+ 1),'B' - 'A' = 1等等。 對於if條件,我使用的是RegExp^表示輸入開始,[A-Z]{1}表示A-Z之一,$表示輸入結束。所以,如果它是一個A-Z,那麼temp會得到一個值,其他任何東西都不會使它在if和你的temp將保持爲0,所以你可以很容易地測試你是否有A-Z。

這就是所有代碼審查,我不會給你解決方案,你必須更努力工作,使用谷歌。如果我給你一切準備好複製粘貼,你將無法享受和學習。

相關問題