Java for 循环 多个变量 [已关闭]

2022-09-05 00:05:40

我不确定为什么我的Java代码无法编译,任何建议都将不胜感激。

   String rank = card.substring(0,1);
    String suit = card.substring(1);
    String cards = "A23456789TJQKDHSCl";
    String[] name = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Diamonds","Hearts","Spades","Clubs"};
    String c ="";
    for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
        if(rank===cards.substring(a,b){
            c+=name[a];
        }


    }
    system.out.println(c);

答案 1
  1. 它是 ,不是 (是 的方法,而不是属性)。cards.length()cards.lengthlengthjava.lang.String

  2. 它是(大写的“s”),而不是。请参阅 java.lang.SystemSystem.outsystem.out

  3. 是的

    for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
    

    for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){
    
  4. 从语法上讲,它不是(双精度等于,不是三重等于;缺少右括号),但是要比较两个字符串是否相等,您需要使用:if(rank == cards.substring(a,b)){if(rank===cards.substring(a,b){equals()if(rank.equals(cards.substring(a,b))){

您可能应该考虑下载Eclipse,它是Java开发的集成开发环境(不仅仅是)。Eclipse 会在您键入时显示错误,并提供修复这些错误的帮助。这使得开始Java开发变得更加容易。


答案 2

取而代之的是:for(int a = 0, b = 1; a<cards.length-1; b=a+1; a++;){

它应该是

for(int a = 0, b = 1; a<cards.length()-1; b=a+1, a++){
                                     ^         ^    ^  
                                     |         |    |  
                                     |         |    |  
            -------------------------------------------Note the changes
           |                    
           v                                                  |
   if(rank==cards.substring(a,b){                             |
-------------------------------------------------------------                                  
|
v
System.out.println(c); //capital S in system

推荐