在我的程序中,我想以“ String Int Int”的格式接受用户的输入。 例如“ F 5 200”。
然后我想将这些值存储到三个不同的变量中。 我将如何去做?
import java.util.Scanner;
public class Test {
public static void main(String args[])
{
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a command");
s = in.nextLine();
String str = s;
String[] arrOfStr = str.split(" ", 3);
for (String a : arrOfStr)
System.out.println(a);
}
}
我已经到了分割字符串并输出结果的地步。但是我不确定如何将输出存储到具有正确数据类型的变量中。
答案 0 :(得分:1)
这三个值在数组arrOfStr中可用。因此,您可以拥有三个变量,并将它们分别分配给arrOfStr [0],arrOfStr [1]和arrOfStr [2]。
因此,在这种情况下,代码段可能类似于以下内容
String[] arrOfStr = str.split(" ", 3);
String c = arrOfStr[0];
int val1 = Integer.parseInt(arrOfStr[1]);
int val2 = Integer.parseInt(arrOfStr[2]);
答案 1 :(得分:1)
它们都是String变量,因此您确实必须知道数据的顺序,以便可以将字符串解析为int类型
WITH
categories AS (
SELECT 1 cid, 'ccc' cname, 'ccc/' curl, 3 csort
UNION SELECT 2, 'bbb', 'bbb/', 2
UNION SELECT 3, 'aaa', 'aaa/', 1
UNION SELECT 4, 'ddd', 'ddd/', 4
),
posts AS (
SELECT 1 pid, 'apples' pname, 'apples.html' purl, 2 cid, '2019-01-02 10:11:12' padded, 1 poffon
UNION SELECT 2, 'orange', 'orange.html', 1, '2019-01-02 10:12:00', 1
UNION SELECT 3, 'grape', 'red-grapes.html', 1, '2019-01-06 10:15:12', 1
UNION SELECT 4, 'banana', 'bannas.html', 2, '2019-01-08 10:19:54', 0
UNION SELECT 5, 'kiwi', 'kiwi-fruit.html', 3, '2019-01-10 10:26:20', 1
),
gallery_photos AS (
SELECT 1 gid, 1 pid, 'img01a.jpg' gsrc, 6 gsort
UNION SELECT 2, 1, 'img01b.jpg', 2
UNION SELECT 3, 1, 'img01c.jpg', 4
UNION SELECT 4, 3, 'img03a.jpg', 2
UNION SELECT 5, 3, 'img03b.jpg', 1
UNION SELECT 6, 3, 'img03c.jpg', 4
UNION SELECT 7, 3, 'img03d.jpg', 3
UNION SELECT 8, 2, 'img02a.jpg', 1
UNION SELECT 9, 1, 'img01d.jpg', 5
UNION SELECT 10, 1, 'img01e.jpg', 3
)
SELECT
c.cid,
c.cname,
c.curl,
g.gsrc,
g.gsort
FROM
categories c
LEFT JOIN (SELECT cid, MAX(padded) padded FROM posts WHERE poffon = 1 GROUP BY cid) pmax ON pmax.cid = c.cid
LEFT JOIN posts p ON p.cid = c.cid AND p.padded = pmax.padded
LEFT JOIN (SELECT pid, MIN(gsort) gsort FROM gallery_photos GROUP BY pid) gmin ON gmin.pid = p.pid
LEFT JOIN gallery_photos g ON g.pid = p.pid AND g.gsort = gmin.gsort
ORDER BY c.cname;