对于我的作业,我必须允许玩家从两个不同的列表中选择6个号码。
List<Integer> large = Arrays.asList(25, 50, 75, 100);
List<Integer> small = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10);
在他们选择了他们的数字[100,3,5,6,9,5]之后,然后他们产生目标数量,例如299,然后他们只能使用选择的数字作为达到目标只使用乘法,加法,减法和除法。所以他们可以输入例如100 * 3 + 5 - 6来达到299目标,这将被检查并得分。
不幸的是,我真的没有太多可以继续下去,我对如何开始这样做有点困惑,我不是在寻找一个直接的答案,也许一些指针或外部帮助会非常感谢。
答案 0 :(得分:1)
这有用吗?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class JavaApplication97 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] s = {1, 2, 3, 4};
List<Integer> large = new ArrayList<>(Arrays.asList(25, 50, 75, 100));
List<Integer> small = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10));
List<Integer> yourNumbers = new ArrayList<>();
int numbersToSelect = 6;
while (numbersToSelect > 0) {
System.out.println("Choose " + numbersToSelect + " numbers from these numbers : " + large + " or " + small);
Integer input = in.nextInt();
boolean isItThere = false;
if (large.contains(input)) {
isItThere = true;
large.remove(input);
} else if (small.contains(input)) {
isItThere = true;
small.remove(input);
}
if (isItThere) {
yourNumbers.add(input);
numbersToSelect--;
System.out.println("Number " + input + " is added");
} else {
System.out.println("There is no such number");
}
}
}
}
示例输出:
Choose 6 numbers from these numbers : [25, 50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
25
Number 25 is added
Choose 5 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
25
There is no such number
Choose 5 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
5
Number 5 is added
Choose 4 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
5
Number 5 is added
Choose 3 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
5
There is no such number
Choose 3 numbers from these numbers : [50, 75, 100] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
100
Number 100 is added
Choose 2 numbers from these numbers : [50, 75] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10]
6
Number 6 is added
Choose 1 numbers from these numbers : [50, 75] or [1, 1, 2, 2, 3, 3, 4, 4, 6, 7, 7, 8, 8, 9, 9, 10, 10]
50
Number 50 is added
答案 1 :(得分:1)
如果我们遵循Bedmas(括号指数除法加法减法),我们可以将其分解为一个简单的函数。
首先将等式转换为组件列表:
100 * 3 + 5 - 6
更改为
["100", "*", "3", "+", "5", "-", "6"]
现在评估每个元素以确保它们有效。 ie)组件列表中的每个值必须在选择列表中或具有值* / + - ,如果有n个,则应该有n-1个syms
为了获得结果,我们可以评估列表,...按照我们的顺序合并num-sym-num部分,按照bedmas的顺序
在伪:
func int compute_val(ListString eqn)
while not eqn.length is 1
if "*" in eqn
index = eqn.getIndex("*")
replace eqn[index -1:index +1] with str((int) eqn[index -1] * (int)eqn[index +1])
else if "/" in eqn
index = eqn.getIndex("/")
replace eqn[index -1:index +1] with str((int) eqn[index -1] / (int)eqn[index +1])
else if "+" in eqn
index = eqn.getIndex("+")
replace eqn[index -1:index +1] with str((int) eqn[index -1] + (int)eqn[index +1])
else if "-" in eqn
index = eqn.getIndex("-")
replace eqn[index -1:index +1] with str((int) eqn[index -1] - (int)eqn[index +1])
return (int)eqn[0]
这将是列表的进展,因为在循环中计算等式
["100", "*", "3", "+", "5", "-", "6"] --> ["300", "+", "5", "-", "6"] -->
["305", "-", "6"] --> ["299"]
答案 2 :(得分:0)
您需要多个步骤,因为我认为这是一项家庭作业,但我不打算给出完整的答案:
虽然2和3是微不足道的,但第一部分对你来说可能是最难的,也是任务的核心。您可以在此处获取有关该任务的更多信息:Smart design of a math parser?