Code Java về Deal or no deal bị lỗi "cannot find symbol, or error unexpected type"

Chào các bạn. Bạn mình gửi 1 file java về Deal or No deal game trên tivi. Mình ko biết cách sửa file này như thế nào vì luôn bị lỗi "cannoy find symbol, hoặc error unexpected type. Mong các cao nhân có thể giúp. Cám on!!!
Class player

public class Player
{
	private Suitcase pickedCase;
	
	public Player(Suitcase suitcase)
	{
		this.pickedCase = suitcase;
	}
	
	public int getSuitcaseValue()
	{
		return pickedCase.getValue();
	}
	
	public String toString()
	{
		return "Player has chosen suitcase " + pickedCase.getLabel() + " with the value " + pickedCase.getValue();
	}
}

Class Suitcase

import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;

public class Suitcase
{
	private static final AtomicInteger count = new AtomicInteger(0); 
	private int label;
	private int value;
	
	public Suitcase (int value)
	{
		this.label = count.incrementAndGet();
		this.value = value;
	}
	
	public int getLabel()
	{
		return this.label;
	}
	
	public int getValue()
	{
		return this.value;
	}
	
	public String toString()
	{
		return "Suitcase " + pickedCase.getLabel() + " with the value " + pickedCase.getValue();
	}
}

class main

import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;

public class Assignment
{
    public static void main ( String [] arguments )
    {
		ArrayList<Suitcase> suitcaseList = new ArrayList<Suitcase>();
		Random rand = new Random();
		List<int> valueList = Lists.newArrayList(20, 50, 100);
	 
		for (int i = 0; i < 3; i++) {
			int randomIndex = rand.nextInt(valueList.size());
			suitcaseList.add(new Suitcase(valueList.get(randomIndex)));
			valueList.remove(randomIndex);
		}
		
		System.out.println("Please select a suitcase: ");
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		
		Player player;
		for(Suitcase pickedCase: suitcaseList)
		{
			if (pickedCase.getLabel() == input)
			{
				player = new Player(pickedCase);
			}
		}
		
		System.out.println(player.toString());
		System.out.println("The bank has: ");
		
		int bankValue = 0;
		for(Suitcase leftoverCase: suitcaseList)
		{
			if (leftoverCase.getLabel() == input)
			{
				bankValue = bankValue + leftoverCase.getValue();
				System.out.println(leftoverCase.toString());
			}
		}
		
		if(player.getSuitcaseValue() < bankValue)
			System.out.println("The banker wins the game");
		else
			System.out.println("The player wins the game");
    }
	
	
}

Đây là kết quả mình muốn có

Lỗi báo dòng nào hả bạn?
Thường thì lỗi này do bạn gọi phương thức hoặc trường chưa có trong trong lớp.

1 Like

Lỗi ở class suitcase và class player toString method,
class main thì ở ArrayList và List… Còn 1 số lỗi mình ko biết vì chưa sửa xong những lỗi trên nên ko biết được những lỗi sau.

Trường pickedCase ở lớp Suitcase không có mà gọi nó trong toString(). Có lẽ do sao chép từ phương thức toString() của lớp Player.
Đổi nó thành:

	public String toString()
	{
		return "Suitcase " + this.getLabel() + " with the value " + this.getValue();
	}
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?