
正文
java水果转盘游戏代码 水果转盘游戏能提现的软件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java程序题!!!求大神指教!谢了! 有一个水果箱(Box),箱子里装
时间不够 我得回宿舍 所有的验证部分都没做 代码可以运行 但前提是你得正确输入 否则不认
package com.Test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
//java程序题!!!求大神指教!谢了!
//有一个水果箱(Box),箱子里装有水果(Fruit),每一种水果都有不同的重量和颜色,
//水果有:苹果,梨,橘子。每个苹果(Apple)都有不同的重量和颜色,
//每个橘子(Orange)有不同的重量和颜色,每个梨(Pear)都有不同的重量和颜色,
//可以像水果箱(Box)里添加水果(addFruit),也可以取出水果(getFruit),
//还可以显示水果的重量和颜色,写出实现这些方法的代码,要求实现上述功能!
public class HelpTest {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
Box b=new Box();
while(true)
{
System.out.println(" 请选择功能 ");
System.out.println("1 添加水果");
System.out.println("2 删除水果");
System.out.println("3 显示水果信息");
System.out.println("4 退出系统");
int a =Integer.parseInt(br.readLine());
System.out.println(a);
switch (a) {
case 1:
System.out.println("请输入水果名称");
String name=br.readLine();
System.out.println("请输入水果颜色");
String color=br.readLine();
System.out.println("请输入水果重量");
float weight=Float.parseFloat(br.readLine());
Furit f=new Furit(name, color, weight);
b.AddFurit(f);
break;
case 2:
System.out.println("请输入水果名称");
String name1=br.readLine();
b.DelFurit(name1);
break;
case 3:
System.out.println("请输入水果名称");
String name2=br.readLine();
b.ShowInfo(name2);
break;
case 4:
System.exit(0);
break;
default:
break;
}
}
}
}
class Furit{
private String name;
private String color;
private float weight;
public Furit(String name,String color,float weight){
this.name=name;
this.color=color;
this.weight=weight;
}
public Furit(String name)
{
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
class Box{
ArrayListFurit al=new ArrayListFurit();
//添加水果的方法
public void AddFurit(Furit f){
al.add(f);
}
//删除水果的方法
public void DelFurit(String name)
{
for(int i=0;ial.size();i++)
{
if(al.get(i).getName().equals(name))
{
al.remove(i);
}
}
}
//显示水果的方法
public void ShowInfo(String name ){
for(int i=0;ial.size();i++)
{
Furit f=(Furit)al.get(i);
if(name.equals(f.getName()))
{
System.out.println(al.get(i).getName()+"的颜色是"+al.get(i).getColor()+" 重量是"+al.get(i).getWeight());
}
}
}
}
我给你写了这么多你采纳他的答案? 呵呵
相关问答
Q1: java-----输入5种水果的英文名称,按照第一个字母排序后输出
具体代码如下,你建一个名为Test.java的文件,将以下代码复制,编译运行即可。
import java.util.Scanner;
public class Test {
public static void main(String [] args)
{
System.out.println("====程序开始执行!====");
while(true)
{
System.out.println("请输入五种水果的名字,中间用逗号隔开:");
Scanner r=new Scanner(System.in);
//接收用户输入
String str=r.next();
//定义正则表达式来检验用户输入格式是否合法
String regex="[a-zA-Z]+,[a-zA-Z]+,[a-zA-Z]+,[a-zA-Z]+,[a-zA-Z]+";
if(!str.matches(regex))
{
System.out.println("您输入的格式不合法,请重新输入");
continue;
}
try
{
System.out.println("您输入的序列为:["+str+"],重新输入请按1,进行排序请按2,退出请按其它任意键");
int flag=r.nextInt();
switch(flag)
{
//提示用户重新输入
case 1:
{
continue;
}
//按第一个字母排序并输出
case 2:
{
String [] tmp=str.split(",");
int length=tmp.length;
//由于只有五种水果,不涉及效率问题,用冒泡排序即可
for(int i=0;ilength-1;i++)
{
for(int j=0;jlength-i-1;j++)
{
if(tmp[j].compareTo(tmp[j+1])0)
{
String strtmp=tmp[j];
tmp[j]=tmp[j+1];
tmp[j+1]=strtmp;
}
}
}
System.out.println("按任意键正序输出排序后结果,如想逆序输出请按n");
String outflag=r.next();
if(outflag.equals("n"))
{
System.out.print("逆序结果为:");
for(int k=length-1;k=0;k--)
{
System.out.print("["+tmp[k]+"]");
}
System.out.println("");
}
else
{
System.out.print("正序结果为:");
for(int k=0;klength;k++)
{
System.out.print("["+tmp[k]+"]");
}
System.out.println("");
}
}
}
//程序执行完毕,退出while循环
break;
}
catch(Exception e)
{
//出现异常 说明用户输入了非数字键,此时退出程序
break;
}
}
System.out.println("====程序执行结束!====");
}
}
Q2: 帮我写java小游戏?
这不就是连连看嘛
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i++) {
for(int j=0;j=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i 6;i++){
for(int j = 0;j 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x+1) {
remove();
}
}
}
}
}
for (i=0;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
Q3: java程序
/**
* 1.定义一个水果类Fruit,符合如下要求:
(a) 类Fruit的成员变量java水果转盘游戏代码;
weight表示水果的质量java水果转盘游戏代码,数据类型为float
color表示水果的颜色,数据类型为string.
(b)类Fruit的成员方法:
getWeight()获得水果的质量
getColor()获得水果的颜色
2、按照第1题中的水果类Fruit的定义,创建该类的对象f1和f2,其中f1的weight值为0.86, color值为yellow;
f2的weight值为0.73, color值为red; 存储并输出f1和f2的值,计算f1和f2的平均值。
* @author Administrator
*
*/
public class Fruit {
private float weight;
private String color;
//构造函数:用于存储对象的2个值
public Fruit(float weight, String color){
this.weight = weight;
this.color = color;
}
public float getWeight() {
return this.weight;
}
public String getColor() {
return this.color;
}
//求重量的平均值
public static float avg(float w1, float w2){
return w1*w2/2;
}
public static void main(String[] args){
Fruit f1 = new Fruit((float) 0.86, "yellow");
Fruit f2 = new Fruit((float) 0.73, "red");
System.out.println("f1:" + "\n" + " weitht:" + f1.getWeight() + "\n" + " color:" + f1.getColor());
System.out.println("f2:" + "\n" + " weitht:" + f2.getWeight() + "\n" + " color:" + f2.getColor());
System.out.println("平均值:" + Fruit.avg(f1.getWeight(), f2.getWeight()));
}
}
Q4: 5种水果,用java来按照字典顺序排列输出
public class Fruit {
String[] fruits=new String[5];
public void inputfruit(){
Scanner input=new Scanner(System.in);
for(int i=0;ifruits.length;i++){
System.out.print("请输入第"+(i+1)+"种水果:");
fruits[i]=input.next();
}
}
public String[] getNames(){
Arrays.sort(fruits);
return fruits;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Fruit sg=new Fruit();
String[] newFruits=new String[5];
sg.inputfruit();
newFruits=sg.getNames();
System.out.println("这些水果在字典种出现java水果转盘游戏代码的顺序是:");
for(int i=0;inewFruits.length;i++){
if(newFruits[i]!=null){
System.out.print(newFruits[i]+"\t");
}
}
}
Q5: java创建类 学渣求解
弄了一下,代码如下,你可以参考参考:
Fruit类:
public abstract class Fruit {
private String shape;
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
public abstract void eat();
}
Apple类:
public class Apple extends Fruit {
public Apple(String shape) {
setFatherShape(shape);
}
public void setFatherShape(String shape)
{
super.setShape(shape);
}
public String getFatherShape()
{
return super.getShape();
}
@Override
public void eat()
{
System.out.println(getFatherShape()+"的苹果好甜");
}
}
Banana类:
public class Banana extends Fruit {
public Banana(String shape) {
setFatherShape(shape);
}
public void setFatherShape(String shape)
{
super.setShape(shape);
}
public String getFatherShape()
{
return super.getShape();
}
@Override
public void eat()
{
System.out.println(getFatherShape()+"的香蕉好香");
}
}
Orange类:
public class Orange extends Fruit {
public Orange(String shape) {
setFatherShape(shape);
}
public void setFatherShape(String shape)
{
super.setShape(shape);
}
public String getFatherShape()
{
return super.getShape();
}
@Override
public void eat()
{
System.out.println(getFatherShape()+"的桔子好酸");
}
}
Game类:
public class Game {
public Fruit luckDraw(){
Random random=new Random();
int luckNum= random.nextInt(3);//随机产生一个0-2之间的数
Fruit fruit = null;
//0-苹果(圆圆的)、1-香蕉(弯弯的)、2-桔子(长长的)
if(luckNum==0){
fruit=new Apple("圆圆的");
}else if (luckNum==1) {
fruit=new Banana("弯弯的");
}else if (luckNum==2) {
fruit=new Orange("长长的");
}
return fruit;
}
public static void main(String[] args) {
Fruit[] fruits=new Fruit[10];
Game game=new Game();
for (int i=0;i10;i++) {
fruits[i]=game.luckDraw();
fruits[i].eat();
}
}
}
运行结果:
弯弯的的香蕉好香
弯弯的的香蕉好香
长长的的桔子好酸
长长的的桔子好酸
圆圆的的苹果好甜
长长的的桔子好酸
圆圆的的苹果好甜
弯弯的的香蕉好香
长长的的桔子好酸
长长的的桔子好酸
楼主若觉得回答有所帮助,望采纳,谢谢!
关于java水果转盘游戏代码和水果转盘游戏能提现的软件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






