loev139
V2EX  ›  Java

equals 语句判断 string,返回值问题

  •  
  •   loev139 · Feb 17, 2016 · 3672 views
    This topic created in 3746 days ago, the information mentioned may be changed or developed.

    代码如下:
    package homw;
    import java.util.*;
    public class capitol_method {
    Scanner input = new Scanner(System.in);

    int done = 0;
    int wrong = 0;
    int correct = 0;
    String[][] spc = {
           {"MONTGOMERY","AlABAMA"},
           {"JUNEAU","ALASKA"}
    

    };

    public void guess (){
    
    
    
    
        for (int i=0; i<50; i++){
    
    
        while (done == 0 && i<50){
        String capitol = input.next();
        String upp2 = capitol.toUpperCase();
    
        if (!spc[i][1].equals(upp2)){
         wrong = wrong + 1;
    
    
        }else{
        correct = correct + 1;
    
        }
      System.out.println("wong"+wrong);
        System.out.println("correct"+correct);
        System.out.println("spc"+spc[i][1]);
        }   
    
    }
    
    }
    
    }
    
    debug 返回的 spc 和输入的一摸一样。但是 wrong 变量的数值却在增加。
    
    5 replies    2016-02-19 08:22:37 +08:00
    hx1997
        1
    hx1997  
       Feb 17, 2016
    ALABAMA not AlABAMA maybe?
    loev139
        2
    loev139  
    OP
       Feb 17, 2016
    @hx1997 我擦,真是,手抖的不行
    yuriko
        3
    yuriko  
       Feb 18, 2016
    写 Java 我好久不用数组了……
    loev139
        4
    loev139  
    OP
       Feb 18, 2016
    @yuriko array 很重要的,你现在写什么程序
    yuriko
        5
    yuriko  
       Feb 19, 2016
    @loev139 绝大多数情况都可以用 List 代替,减少了很多索引处理上的问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1496 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 16:51 · PVG 00:51 · LAX 09:51 · JFK 12:51
    ♥ Do have faith in what you're doing.