添加了整数值,并在不同的类使用它们

问题描述 投票:0回答:1

我想提出一些复习一个有趣的练习剧本,但我所遇到的一些问题。该脚本使用随机数的字母“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倍,而不是和我似乎无法弄清楚如何使之出现一次。所有的帮助是非常赞赏。谢谢。

loops methods increment
1个回答
0
投票

我最终改变剧本颇有几分到底有没有必要为一个ArrayList(我真的不明白增强环路)。以下是我结束了

package Yahtzee;

import java.util.Scanner;

public class Test1 {

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();

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

        System.out.println("Congratulations you got " + Test2.rv + " Yahtzees");
    }
}


package Yahtzee;

import java.util.Random;

public class Test2 {
public String a;
public String b;
public String c;
public static int rv;

public Test2()
    {
    System.out.println("");
    a = method1();
    b = method1();
    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");
    if(a == "A" && b == "A" && c == "A")
        {
        System.out.println("YAHTZEE!");
        rv = (rv + 1); 
        }
    else if(a == "B" && b == "B" && c == "B")
        {
        System.out.println("YAHTZEE!");
        rv = (rv + 1); 
        }
    else if(a == "C" && b == "C" && c == "C")
        {
        System.out.println("YAHTZEE!");
        rv = (rv + 1); 
        }   
    }

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;
        }

}
© www.soinside.com 2019 - 2024. All rights reserved.