我正在研究一种将二进制数字转换为其相应的单词值的代码。
例如,我输入“3”,代码会将数字转换为“11”,这是“3”的二进制表示形式。该代码将继续将“11”转换为“一一”并输出。
我已经写了二进制转换部分,但我很难将其转换为单词。
public class BinaryWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String S = sc.nextLine(); //how many times the for loop will repeat
for (int i = 0; i < S.length() + 1; i++) {
int A = sc.nextInt(); //input the number
String convert = Integer.toBinaryString(A); //converts the number to binary String
String replace = convert.replaceAll("[1 0]", "one, zero "); //replaces the String to its value in words …Run Code Online (Sandbox Code Playgroud) 我目前正在研究一个简单的代码,它将检查用户输入的String是否包含for循环中指定的字符.
我目前的代码
import java.util.Scanner;
public class AutumnLeaves {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int G = 0;
int R = 0;
int Y = 0;
int B = 0;
String S = sc.nextLine();
for (int i = 0; i < S.length(); i++) {
if (S.contains("G")) {
G++;
} else {
if (S.contains("R")) {
R++;
} else {
if (S.contains("Y")) {
Y++;
} else {
if (S.contains("B")) {
B++;
}
}
}
}
}
int …Run Code Online (Sandbox Code Playgroud) 我发现某些输入没有存储在 Python 3 的字典中。
运行此代码:
N = int(input()) #How many lines of subsequent input
graph = {}
for n in range (N):
start, end, cost = input().split()
graph[start] = {end:cost}
#print("from", start, ":", graph[start])
print(graph)
Run Code Online (Sandbox Code Playgroud)
带输入:
3
YYZ SEA 500
YYZ YVR 300
YVR SEA 100
Run Code Online (Sandbox Code Playgroud)
我的程序输出:
{'YYZ': {'YVR': '300'}, 'YVR': {'SEA': '100'}}
Run Code Online (Sandbox Code Playgroud)
似乎第一行提到 YYZ 被第二行提到 YYZ 覆盖,因为字典中没有 SEA 的痕迹。
是什么导致了这个问题,我该如何解决?