JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區 » 演算法  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 詢問JAVA ERROR MASSAGE
sage1698





發文: 3
積分: 0
於 2019-07-30 16:33 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package Com.Pcsetting.Example;
    import java.io.*;
    import java.util.Scanner;
 
public class Exercise_5_3 {
 
  public static void main(String[] args) {
  
    System.out.println("請輸入成績:");
    String str1; 
                Scanner sc = new Scanner(System.in);    
    str1 = sc.nextLine();
    System.out.println("您輸入成績為:" + str1 );
    
    switch (str1) {
    case "A":
      if(str1>=90&&str1<100)
      System.out.println("A");
    break;  
    case "B":
      if(str1>=80&&str1<90)
      System.out.println("B");
    break;    
    case "C":
      if(str1>=70&&str1<80)
      System.out.println("C");
    break;  
    case "D":
      if(str1>=60&&str1<70)
      System.out.println("D");
    break;  
    case "E":
      if(str1<60
      System.out.println("E");
    break;  
 
    }
  }
 
}
 


第一次的發言,想請問一下我所寫的這段程式目前在各if的地方出現error
"未對引數類型 String, int 定義運算子 <"
可是當我將宣告改成int時error卻又變成在case的地方
"類型不符:無法從 String 轉換為 int"
想請問是哪個地方有誤呢??


reply to postreply to post
作者 Re:詢問JAVA ERROR MASSAGE [Re:sage1698]
weber





發文: 195
積分: 0
於 2019-07-31 08:20 user profilesend a private message to usersend email to weberreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1.String 要轉化為int才能用於計算,
2.程式邏輯有問題, 應不需要用case, 只要用if即可,
例如:
1
2
3
int score=Integer.parseInt(str1);
if(score>=90 && score<100) System.out.println("A");
else if(score>=80 && score<90) System.out.println("B");


reply to postreply to post
作者 Re:詢問JAVA ERROR MASSAGE [Re:sage1698]
kentyeh





發文: 644
積分: 6
於 2019-07-31 09:17 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
太多的if else 不是好方式,而且沒有錯誤控制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.InputMismatchException;
import java.util.Scanner;
 
public class Exercise {
    public static void main(String[] args) {
        int[] margin = new int[]{90, 80, 70, 60, 1};
        char[] grade = new char[]{'A', 'B', 'C', 'D', 'E'};
        Scanner sc = new Scanner(System.in);
        int score = -1;
        do {
            try {
                System.out.println("請輸入成績:");
                score = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("無效的成績");
            }
            sc.nextLine();
        } while (score < 0);
        for (int i = 0; i < margin.length; i++) {
            if (score / margin[i] > 0) {
                System.out.printf("您輸入成績為:(%c)%d\n", grade[i], score);
                break;
            }
        }
    }
}


reply to postreply to post
作者 Re:詢問JAVA ERROR MASSAGE [Re:weber]
sage1698





發文: 3
積分: 0
於 2019-07-31 09:46 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝大大的解釋,這是書中的練習題
使用if的運算是我已經有做出來了,
但下一個練習題要用switch多條件
進行練習,繞不出來所已進行發問...


reply to postreply to post
作者 Re:詢問JAVA ERROR MASSAGE [Re:sage1698]
weber





發文: 195
積分: 0
於 2019-07-31 15:17 user profilesend a private message to usersend email to weberreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
若規定要用到switch, 可在換算後使用, 例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int score=sc.nextInt();
score=score-50;
if (score<0) score=0;
score=score/10;
switch(score){
  case 4:
     System.out.println("A"); 
     break;
  case 3:
     System.out.println("B"); 
     break;
  case 2:
     System.out.println("C"); 
     break;
  case 1:
     System.out.println("D"); 
     break;
  case 0:
     System.out.println("E"); 
     break;
  }


weber edited on 2019-07-31 21:37
reply to postreply to post
作者 Re:詢問JAVA ERROR MASSAGE [Re:weber]
sage1698





發文: 3
積分: 0
於 2019-08-01 09:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝大大的解惑,可以了謝謝您~~~

reply to postreply to post
» JWorld@TW »  Java 新手區 » 演算法

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8