Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

svetlyoek avatar svetlyoek 20 Точки

задача 03.Gaming Store-More Excercise

Моля за решение,че нещо не си откривам грешката:

Write a program, which helps you buy the games. The valid games are the following games in this table:

Name

Price

OutFall 4

$39.99

CS: OG

$15.99

Zplinter Zell

$19.99

Honored 2

$59.99

RoverWatch

$29.99

RoverWatch Origins Edition

$39.99

On the first line, you will receive your current balance – a floating-point number in the range [0.00…5000.00].

Until you receive the command “Game Time”, you have to keep buying games. When a game is bought, the user’s balance decreases by the price of the game.

Additionally, the program should obey the following conditions:

  • If a game the user is trying to buy is not present in the table above, print “Not Found” and read the next line.
  • If at any point, the user has $0 left, print “Out of money!” and end the program.
  • Alternatively, if the user is trying to buy a game which they can’t afford, print “Too Expensive” and read the next line.
  • If the game exists and the player has the money for it, print “Bought {nameOfGame}”

When you receive “Game Time”, print the user’s remaining money and total spent on games, rounded to the 2nd decimal place.

Examples

Input

Output

120

RoverWatch

Honored 2

Game Time

Bought RoverWatch

Bought Honored 2

Total spent: $89.98. Remaining: $30.02

19.99

Reimen origin

RoverWatch

Zplinter Zell

Game Time

Not Found

Too Expensive

Bought Zplinter Zell

Out of money!

79.99

OutFall 4

RoverWatch Origins Edition

Game Time

Bought OutFall 4

Bought RoverWatch Origins Edition

Total spent: $79.98. Remaining: $0.01

Тагове:
0
Fundamentals Module
Tsenkow avatar Tsenkow 16 Точки

https://pastebin.com/A79myctE

Може би си решавам с double, а не с decimal.

Или проверките ти в последните if-ове не са довършени. Сравни си решението с моя код.

0
svetlyoek avatar svetlyoek 20 Точки

Благодаря,но това е друга задача.

0
RuskoKumovski avatar RuskoKumovski 3 Точки

https://pastebin.com/u8yMK0Kf

Това е доста дълъг, но вероятно по-лесно разбираем вариант. Във видеото от Exrs някъде към края, мисля че я решихме с по-малко код. :)

0
RuskoKumovski avatar RuskoKumovski 3 Точки

По-краткия вариант реално е да сетваш само цената в switch-а, а отдолу да направиш проверката и съответно става само един if.

0
Dimitar_Stanchev avatar Dimitar_Stanchev 2 Точки

Здравейте,

Ето едно решение от мен https://pastebin.com/NYj9kpip. 

0
Hristo13 avatar Hristo13 227 Точки

Ето и едно от Java-рите

https://pastebin.com/pcJtfZ6k

0
svetlyoek avatar svetlyoek 20 Точки

Благодаря на всички!

0
VasilK avatar VasilK 40 Точки

За java с hashtable.

 

import java.util.*;

public class Game_Store {
    public static void main(String[] args) {

        Scanner scanner = new Scanner( System.in );
        double bud = Double.parseDouble( scanner.nextLine());
        double money = bud;

        Dictionary<String, Double> dic = new Hashtable<String, Double>();

        dic.put( "OutFall 4", 39.99 );
        dic.put( "CS: OG", 15.99 );
        dic.put( "Zplinter Zell", 19.99 );
        dic.put( "Honored 2", 59.99 );
        dic.put( "RoverWatch", 29.99 );
        dic.put( "RoverWatch Origins Edition", 39.99 );

        while (true) {

            Enumeration enu = dic.keys();
            String game = scanner.nextLine();

            if (money <= 0) {
                System.out.println( "Out of money!" );
                return;
            }

            if (game.equals( "Game Time" )) break;

            while (enu.hasMoreElements()) {

                String str = (String) enu.nextElement();

                if (((Hashtable<String, Double>) dic).containsKey( game ) == false) {
                    System.out.println( "Not Found" );
                    break;
                } else if (str.equals( game ) && money - dic.get( str ) >= 0) {
                        money -= dic.get( str );
                        System.out.printf( "Bought %s \n", game );
                } else if (str.equals( game ) && money - dic.get( str ) < 0) {
                    System.out.println( "Too Expensive" );
                }

            }

        }

        System.out.printf( "Total spent: $%.2f. Remaining: $%.2f", bud - money,  money);
    }
    
}

0
KaloyanGanchev avatar KaloyanGanchev 17 Точки

Здравейте,

ето моето примерно решение със switch конструкция в while цикъл и if проверки тук.

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.