所以我有一个功课,我必须制作一个程序,将数字改为罗马数字。我想使用开关盒,但我不想写出1-3999中的每一个号码。我被告知我们可以通过将案例分别更改为不同的数字来实现,但我不明白这是如何工作的这就是我对那些地方和数十个地方所拥有的但是我怎么把它们放在一起?
public class Number {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a number between 1 and 3999: ");
int number= scan.nextInt();
String numberString;
switch (number%10)
{
case 1: numberString = "I";
break;
case 2: numberString = "II";
break;
case 3: numberString = "III";
break;
case 4: numberString = "IV";
break;
case 5: numberString = "V";
break;
case 6: numberString = "VI";
break;
case 7: numberString = "VII";
break;
case 8: numberString = "VIII";
break;
case 9: numberString = "IX";
break;
default: numberString = "Invalid nummber";
break;
}
switch ((number%100)/10)
{
case 1: numberString = "X";
break;
case 2: numberString = "XX";
break;
case 3: numberString = "XXX";
break;
case 4: numberString = "XL";
break;
case 5: numberString = "L";
break;
case 6: numberString = "LX";
break;
case 7: numberString = "LXX";
break;
case 8: numberString = "LXXX";
break;
case 9: numberString = "XC";
break;
default: numberString = "Invalid nummber";
break;
}
System.out.println(numberString);
}
}
答案 0 :(得分:0)
如果我理解了您的问题,那么您可以交换switch
的顺序并使用String
连接。像,
String numberString = "";
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
default: numberString = "Invalid nummber";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
default: numberString = "Invalid nummber";
break;
}
System.out.println(numberString);
答案 1 :(得分:0)
当你到达第二个开关盒时,你重写了变量numberString。例如,当我给它转换数字18时,第一个开关案例将numberString分配给值“VIII”。但后来我转到第二个开关盒,然后用值“X”覆盖,然后打印出来。在您的第二个开关案例中,您应该使用
case 1: numberString += "X";
break;
此外,您应该切换执行switch语句的顺序。首先使用您的开关((编号%100)/ 10),然后使用另一秒。这将把X的数量放在每个数字的前面。
答案 2 :(得分:0)
如果不使用开关,这个解决方案怎么样?它运作得很好。
# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.
# Copyright (C) 1996-2015 Free Software Foundation, Inc.
# Copying and distribution of this file, with or without modification,
# are permitted provided the copyright notice and this notice are preserved.
# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.
# Below, there should be one TERM entry for each termtype that is colorizable
TERM Eterm
TERM ansi
TERM color-xterm
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM mach-color
TERM mach-gnu-color
TERM mlterm
TERM putty
TERM putty-256color
....
TERM vt100
TERM xterm
TERM xterm-16color
TERM xterm-256color
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#NORMAL 00 # no color code at all
#FILE 00 # regular file: use no color at all
RESET 0 # reset to "normal" color
DIR 01;36 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
# numerical value, the color is as for the file pointed to.)
MULTIHARDLINK 00 # regular file with more than one link
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file ...
MISSING 00 # ... and the files they point to
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
# This is for files with execute permission:
EXEC 01;32
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# archives or compressed (bright red)
.tar 01;31
.tgz 01;31
.arc 01;31
.arj 01;31
.taz 01;31
.lha 01;31
.lz4 01;31
.lzh 01;31
.lzma 01;31
.tlz 01;31
.txz 01;31