累加整数值并在不同分类中使用它们

时间:2019-01-30 00:13:23

标签: loops methods increment

我正在制作一个有趣的练习脚本以进行回顾,但是遇到一些问题。该脚本使用随机数来确定字母“ A,B或C”之间的位置,当您获得3个字母时将显示Yahtzee!在控制台上。我可以让它正常工作,但决定增加25个中的Yahtzees。这是我到目前为止所拥有的。

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import ArrayList.EnhancedLoop2;

public class Test {

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

    Scanner in = new Scanner(System.in);

    System.out.println("TIME TO PLAY JAVA YAHTZEE");
    System.out.println("Type 1 when ready");

    in.nextInt();

    ArrayList<NewClass> al = new ArrayList<NewClass>();

    for(int i = 0; i <= 25; i++)
        {   
        NewClass nw = new NewClass();   

        al.add(nw);
        }

        for(NewClass enhanced : al)

        {
        System.out.println("You got " + enhanced.m + " Yahtzees. Good Job");
        }
    }
}


import java.util.ArrayList;
import java.util.Random;

public class NewClass {

    public String a;
    public String b;
    public String c;
    public static int m;

    public NewClass()
    {
    getLetter();
    }

    public static String getLetter()
        {
                String rv = "";
                System.out.println("");
                String a = method1();
                String b = method1();
                String c = method1();
                System.out.println("Your letters are");
                System.out.println(a + "\n" + b + "\n" + c);
                System.out.print("your set is: " + a + b + c + "\n");
                getLetter2(a, b, c);
                return rv;
                }
    public static String getLetter2(String a, String b, String c)
            {
                String rv = "";
                if(a == "A" && b == "A" && c == "A")
                    {
                    System.out.println("YAHTZEE!");
                    }
                else if(a == "B" && b == "B" && c == "B")
                    {
                    System.out.println("YAHTZEE!");
                    }
                else if(a == "C" && b == "C" && c == "C")
                    {
                    System.out.println("YAHTZEE!");
                    m = yahtzeeCount(a, b, c);
                    }
                return rv;      
            }

public static String method1()
{   
    String letter = "";
    Random r = new Random();
    for(int i = 0; i <= 2; i++)
    {
        int cv = r.nextInt(9) + 1; 

        if(cv <= 3)
        {
            letter = "A";
        }
        else if(cv >= 4 && cv <= 6) 
        {       
            letter = "B";
        }
        else if(cv >=7 && cv <=9)
        {
            letter = "C";
        }
    }
    return letter;
}

public static int yahtzeeCount(String a, String b, String c)
    {
        int rv = 0;
        if(a == "A" && b == "A" && c == "A"  || a == "B" && b == "B" && c ==     "B" || a == "C" && b == "C" && c == "C")
        {
            rv = 1;
        }
        return rv;
    }
}

我的脚本也出现问题,显示“您有#yahtzees。干得好。” 25次而不是一次,我似乎无法弄清楚如何使其仅显示一次。 非常感谢所有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我结束了对脚本的相当多的更改,最终不需要ArrayList(我不太了解增强的循环)。这就是我最后得到的

    background: url(/assets/images/some.svg);