TclOO:类积分

时间:2018-09-05 18:32:02

标签: class tcl

这是我的第一堂课,我的方向正确吗?我主要通过 init ()方法问自己一个问题。没关系 ?我不确定...我在寻找一些建议,方法...

import java.util.*;
public class bmahipat_C5Lab1 {

    public static void main(String[] args) {
        Scanner reader = new Scanner (System.in);
        int player = 0, computer = 0;
        int computerScore = 0, playerScore = 0;
        int loops = 0;

        int rock = 1;
        int paper = 2;
        int scissors = 3;

        for(int i=1; i<6;i++) {
            computer = (int) (Math.random() * 3) + 1;
            System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
            player = reader.nextInt();

            if (player > scissors) {
                System.out.println("Not a valid response");
                System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
                player = reader.nextInt();
                i = -1;
            }

            if (player == computer) {
                System.out.println("Tie");
            } else if (player == rock ) {
                if(computer == paper){
                    System.out.println ("Player picked Rock, Computer picked Paper, Computer wins");
                    computerScore = +1;
                } else if(computer == scissors) {
                    System.out.println ("Player picked Rock, Computer picked Scissors, Player wins");
                    playerScore = +1;
                }    
            } else if (player == paper) {
                if(computer == rock){
                    System.out.println ("Player picked Paper, Computer picked Rock , Player wins");
                    playerScore = +1;
                } else if(computer == scissors) {
                    System.out.println ("Player picked Paper, Computer picked Scissors, Computer wins");
                    computerScore = +1;
                }   
            } else if (player == scissors) {
                if(computer == rock) {
                    System.out.println ("Player picked Scissors, Computer picked Rock , Computer wins");
                    computerScore = +1;
                } else if(computer == paper) {
                    System.out.println ("Player picked Scissors, Computer picked Paper, Player wins");
                    playerScore = +1;
                }
            }
        }

        System.out.println("");
        System.out.println("Computer Wins " + computerScore);
        System.out.println("Player Wins " + playerScore);
    }
}

1 个答案:

答案 0 :(得分:2)

拥有一个构造函数而不是一个init方法可能会更好:

constructor val {
    lassign $val x y z
}

set P0 [Point new {8.2 30 40}]

constructor args {
    lassign $args x y z
}

set P0 [Point new 8.2 30 40]

OOP方面,没有坐标的点没有多大意义,并且您可能不想通过更改坐标来“移动”该点,因此在创建点时应分配坐标,并替换该点如果拥有重点的东西移动了,就换一个新的东西。

通过formatPoint方法更改坐标值是一个好主意吗?为什么不让坐标保留其值并提供格式化的访问权限?

method formatPoint {} {
    format {%.4f %.4f %.4f} $x $y $z
}

坐标访问方法略有偏离。试试

method x {} {
    set x
}

相反:set的一元形式返回值。或者

method x {} {
    return $x
}

如果您愿意。

计算得出正确的结果(〜36.0789),但请注意,您无需在表达式的括号内转义行尾(因为行尾已经被括号转义了)。