【java】笔记10:类与对象——本章练习
题目1:
代码如下:
import java.util.Scanner;
public class Input{
public static void main(String[]args){
Circle c=new Circle();
PassObject yuan=new PassObject();
System.out.println("r"+"\t"+"times");
yuan.printAreas(c,5);
}
}
class Circle{
double pi=Math.PI;
double r;
public double suu(double r){
double s=pi*r*r;
return s;
}
}
class PassObject{
public void printAreas(Circle c,int times)
{
for(int i=1;i<6;i++)
{
System.out.println((double)i+"\t"+new Circle().suu(i));
}
}
}
题目2:
代码如下:
import java.util.Scanner;
import java.util.Random;
public class Input{
public static void main(String[]args){
Scanner myScanner = new Scanner(System.in);
int[][]a=new int[3][3];
String[]b=new String[3];
for(int i=0;i<3;i++)
{
System.out.println("请输入你要出的拳(0-拳头,1-剪刀,2-布):");
int your=myScanner.nextInt();
int n = new Tom().panduan(your);
if(n==0) your=myScanner.nextInt();
Random random=new Random();
int comp=random.nextInt(3);
b[i]=new Tom().sit(your,comp);
System.out.println("===========================");
System.out.println("局数"+"\t玩家的出拳"+"\t电脑的出拳"+"\t输赢情况");
System.out.println(i+1+"\t"+your+"\t\t"+comp+"\t\t"+b[i]);
System.out.println("===========================");
a[i][0]=i+1;
a[i][1]=your;
a[i][2]=comp;
}
System.out.println("局数"+"\t\t玩家的出拳"+"\t电脑的出拳"+"\t输赢情况");
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
{System.out.print(a[i][j]+"\t\t");}
System.out.print(b[i]);
System.out.println();
}
}
}
class Tom{
public String sit(int your,int comp){
if(your==0&&comp==2) return "你赢了";
else if(your==1&&comp==0) return "你赢了";
else if(your==2&&comp==1) return "你赢了";
else if(your==comp) return "平局";
else return "你输了";
}
public int panduan(int num){
if(num>2||num<0)
{
System.out.println("你的输入有误,请输入0-2之间的数");
return 0;
}
else return 1;
}
}
结果如下: