在给出String输入后程序退出

时间:2016-04-29 12:24:04

标签: java input

/**
 * CSCI213 Assignment 1
 * ------------------
 * File name: Player.java
 * Author: Kanak Priya Selvarani
 * UOW Student number: 4641619
 * Description: Player class. This class contains player's details which will be saved in players.dat file.
 */
//package assignment1_kanakpriyaselvarani;
package admin;
import admin.User;
import java.util.ArrayList;
//import User.java;

/**
 *
 * @author SONY
 */
public class Player extends User{
    private int scores;
    private String date;
    //private String loginName, hashedPassword;
    ArrayList<Card>cardsOnHand;

    public Player(String loginName, String hashPass, String date, int scores)
    {
        super(loginName,hashPass);
        //this.loginName = loginName;
        //this.hashedPassword = hashedPassword;
        this.scores = scores;
        this.date = date;
        cardsOnHand = new ArrayList<>();
    }

    public String getDate()
    {
        return date;
    }

    public int getScores()
    {
        return scores;
    }

    public void addScores(int scores)
    {
        this.scores+=scores;
    }

    public void deductScores(int scores)
    {
        this.scores-=scores;
    }

    public void setScores(int scores)
    {
        this.scores=scores;
    }

    public int  getNumOfCards()
    {
        return cardsOnHand.size();
    }

    public void addCard(Card card)
    {
        cardsOnHand.add(card);
    }

    public void showCards()
    {
        System.out.println(getLoginName());
        for(Card c:cardsOnHand)
        {
            System.out.print(c+ " ");
        }
        System.out.println("");
    }

    public void showCardsValue()
    {
        System.out.println("Value:"+getTotalCardsValue());
    }

    public int getTotalCardsValue()
    {
        int totalValue=0;

        for(Card c:cardsOnHand)
        {  
            totalValue+=c.getValue();
        }

        return totalValue;        
    }

    //Thinks of the action that a player can take, implements those action as methods...



    public static void main(String []args)
    {
        //testing of getTotalCardsValue()    
        Player p = new Player("tester","","",0);

        Card c1 = new Card("Heart","Ace",1);
        Card c2 = new Card("Heart","Queen",10);

        p.addCard(c1);
        p.addCard(c2);

        p.showCards();
        p.showCardsValue();

        Card c3 = new Card("Heart","3",3);
        p.addCard(c3);
        p.showCards();
        p.showCardsValue();


    }
}

我想找一个数组位置。给定输入时,程序自动退出。如果我将值赋给n,它就完美了。我想知道为什么程序在输入后会退出。

1 个答案:

答案 0 :(得分:1)

改变这个:

if(n==l1.get(j))

到此:

if(n.equals(l1.get(j)))

原因是==检查引用,但是equals将检查字符串是否相等。