14章 上机2

public class Cus {
	String[] names = new String[5];

	public void add(String name) {
		for (int i = 0; i < names.length; i++) {
			if (names[i] == null) {
				names[i] = name;
				break;
			}
		}

	}
public void show(){
	for(int i=0;i<names.length;i++){
		System.out.print(names[i]+" ");
		}
}
public boolean e(String jiuname,String newname){
	boolean f=false;
	for(int i=0;i<names.length;i++){
		if(names[i].equals(jiuname)){
			names[i]=newname;
			f=true;
			break;
		}
		
	}
	return f;
			
}
}

import java.util.Scanner;


public class Cus2 {

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

		
		 Cus s=new Cus();
			Scanner input=new Scanner(System.in);
			boolean c=true;
			while(c){
				
				System.out.println("输入客户姓名:");
				String newname=input.next();
				s.add(newname);
				System.out.println("继续输入?(y/n)");
				String j=input.next();
				if(j.equals("n")){
					c=false;
				}
			}
			System.out.println("客户姓名列表:");
			s.show();
			System.out.println("\n输入旧姓名:");
			
			String jiuname=input.next();
			System.out.println("\n输入新姓名:");
			String  newname=input.next();
			System.out.println("******结果****");
			if(s.e(jiuname, newname)){
				System.out.println("找到了");
				s.show();
			}else{
				System.out.println("没有找到");
			}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。实现扑克牌比大小的游戏需要以下几个步骤: 1. 定义扑克牌的数据结构 可以使用一个二维数组来表示一副扑克牌,其中第一维表示牌的花色,第二维表示牌的点数。例如,可以用0-3表示黑桃、红桃、梅花、方块,用1-13表示A、2、3、...、10、J、Q、K。 2. 洗牌 将一副扑克牌按照随机顺序洗牌,可以使用随机数来实现。 3. 发牌 将洗好的扑克牌发给玩家和庄家,每人发两张牌。 4. 比较大小 根据扑克牌的点数来比较大小,A算1点,J、Q、K算10点,其他牌按照点数算。如果玩家或庄家的牌点数超过21点,则认为该玩家输了;如果点数相同,则平局;否则点数大的获胜。 下面是一个简单的实现示例: ```python import random # 定义一副扑克牌 deck = [[i, j] for i in range(4) for j in range(1, 14)] # 定义比较大小的函数 def compare(cards): total = sum([min(card[1], 10) for card in cards]) if total > 21: return -1 else: return total # 洗牌 random.shuffle(deck) # 发牌 player_cards = [deck.pop(), deck.pop()] dealer_cards = [deck.pop(), deck.pop()] # 显示玩家的牌和庄家的第一张牌 print("Your cards:", player_cards) print("Dealer's cards:", [dealer_cards[0], ['*', '*']]) # 玩家决定是否要继续要牌 while True: choice = input("Do you want to hit or stand? (h/s)") if choice == 'h': player_cards.append(deck.pop()) print("Your cards:", player_cards) if compare(player_cards) == -1: print("You lose!") break else: break # 庄家开始要牌 while compare(dealer_cards) < 17: dealer_cards.append(deck.pop()) # 显示庄家的牌 print("Dealer's cards:", dealer_cards) # 比较大小 player_score = compare(player_cards) dealer_score = compare(dealer_cards) if player_score == -1: print("You lose!") elif dealer_score == -1 or player_score > dealer_score: print("You win!") elif player_score == dealer_score: print("Tie!") else: print("You lose!") ``` 希望这个示例能够帮助你实现扑克牌比大小的游戏!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值