В моей программе я пытаюсь использовать сканер для сканирования файла, полного целых чисел. Это домашнее задание с просьбой написать программу, в которой показаны все способы получения предопределенной суммы денег с данными монетами, а тестер использует такие файлы.
// Coins available in the USA, given in cents. Change for $1.43?
1 5 10 25 50 100
143
Мой вывод должен иметь самую последнюю строку (строка, представляющая общую сумму денег ex: 143), чтобы выглядеть следующим образом:
change: 143
1 x 100 plus 1 x 25 plus 1 x 10 plus 1 x 5 plus 3 x 1
1 x 100 plus 0 x 25 plus 4 x 10 plus 0 x 5 plus 3 x 1
1 x 100 plus 0 x 25 plus 3 x 10 plus 2 x 5 plus 3 x 1
1 x 100 plus 0 x 25 plus 2 x 10 plus 4 x 5 plus 3 x 1
1 x 100 plus 0 x 25 plus 1 x 10 plus 6 x 5 plus 3 x 1
1 x 100 plus 0 x 25 plus 0 x 10 plus 8 x 5 plus 3 x 1
2 x 50 plus 1 x 25 plus 1 x 10 plus 1 x 5 plus 3 x 1
2 x 50 plus 0 x 25 plus 4 x 10 plus 0 x 5 plus 3 x 1
...
моя борьба в том, что у меня есть инициализированная переменная,
Integer change;
и я установил
change = input.nextLine();
Тем не менее, я получаю это сообщение об ошибке, заявляя, что это несовместимый тип, требующий String. Как сделать это, где я могу сканировать следующую строку и установить ее в целое число? Спасибо за любую помощь!
Разделите строку на Integer change = Integer.parseInt(input.nextLine());
Это сканер с Java? Если да, попробуй это. , ,
Scanner scantron = new Scanner( 'input file' );
// can be dynamically added to easier than normal arrays
ArrayList<Integer> coins = new ArrayList<Integer>();
int change;
// toggle flag for switching from coins to change
boolean flag = true;
while(scantron.hasNextLine())
{
// if this line has no numbers on it loop back to the start
if(!scantron.hasNextInt()) continue;
// getting the first line of numbers
while(flag && scantron.hasNextInt()) coins.add(scantron.nextInt());
// set the flag that the coins have been added
flag = false;
// if this is the first time the flag has been seen ignore this
// otherwise the next line should have the change
if(!flag) change = scantron.nextInt();
}
1 5 10 25 50 100
или143