简体   繁体   中英

infinite execution of while loop in java

My problem statement: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are n monsters you'll need to defeat in this quest. Each monster i is described with two integer numbers - power(i) and bonus(i). To defeat this monster, you'll need at least power(i) experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonus(i) experience points if you defeat this monster. You can defeat monsters in any order.

Input: The first line contains an integer, n, denoting the number of monsters. The next line contains an integer, e, denoting your initial experience. Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, power(i), which represents power of the corresponding monster. Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonus(i), which represents bonus for defeating the corresponding monster.

Sample cases1:

Input

2

123

78

130

10

0

OUTPUT:

2

EXPLANATION: Initial experience level is 123 points. Defeat the first monster having power of 78 and bonus of 10. Experience level is now 123+10=133. Defeat the second monster.

SECOND CASE

INPUT

3

100

101

100

304

100

1

524

OUTPUT:

2

EXPLANATION: Initial experience level is 100 points. Defeat the second monster having power of 100 and bonus of 1. Experience level is now 100+1=101. Defeat the first monster having power of 101 and bonus of 100. Experience level is now 101+100=201. The third monster can't be defeated

THIS IS MY CODE

import java.util.*;
public class Main {

    public static int cal(int a[],int e,int n){
        
        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();

        for(int i=0;i<n;i++){
            if(a[i]<= e && !(map.containsValue(a[i]))){
                map.put(1,a[i]);
                return i;
            }

        }

        return -1;
    }
    public static void main(String[] args) {
        int n,e,flag=0,count=0;

        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of monster");
        n=sc.nextInt();
        System.out.println("Enter the experience");
        e=sc.nextInt();
        int a[]=new int[n];
        int b[]=new int[n];
        System.out.println("Enter the monster power");
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
        System.out.println("enter the bonus");
        for(int j=0;j<n;j++) {
            b[j] = sc.nextInt();
        }
        while(flag!=-1){
            flag=cal(a,e,n);
            System.out.println(flag);
            if(flag!=-1)
            {

                e=e+b[flag];
                count++;
            }
        }
        System.out.println(count);
    }
}

cal function is calling infintely Can you help me out!!!

There's a problem with the (flag!=-1) condition.
Your cal function has a condition that is always true: !(map.containsValue(a[i]) , as assigning the key 1 to every value that is less than or equal to e leads you to the substitution of the previous value associated with the key 1. This is due to the fact that in a Map a key can only be associated with at most one value, and the method V put(K key, V value) just replaces the already present value with the new one (and, in fact, returns the value that's been replaced.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM