-
2021-02-12 13:39:18
华为2019.9.18笔试第一题:
判断数据是否合理,给了三种合理的情况,一个是两位数与一位数交替出现,一个是两头是两位数,中间全是一位数,最后一种情况是两头是一位数,中间全是两位数。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
List list = new ArrayList();
String s;
while(true){
if((s = scanner.nextLine()).equals("")){
break;
}
list.add(s);
}
for(int i = 0; i < list.size(); i++){
String string = list.get(i);
String temp[] = string.split(" ");
int res[] = new int[temp.length];
for(int j = 0; j < temp.length; j++){
res[j] = Integer.parseInt(temp[j]);
}
System.out.print(isequ(res) + " ");
}
}
public static boolean isequ(int a[]) {
if(a[0] >= 10 && a[a.length - 1] >= 10){
for(int i = 1; i < a.length - 1; i++){
if(a[i] >= 10){
return false;
}
}
}else if (a[0] < 10 && a[a.length - 1] < 10) {
for(int i = 1; i < a.length - 1; i++){
if(a[i] < 10){
return false;
}
}
}else {
for(int i = 0; i < a.length; i++){
if(i % 2 == 0 && a[i] >= 10){
return false;
}else if(i % 2 == 1 && a[i] < 10){
return false;
}
}
}
return true;
}
}
更多相关内容 -
Java 输入多行字符串或者多个int数值的方法
2020-08-27 04:24:54今天小编就为大家分享一篇Java 输入多行字符串或者多个int数值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
Java 输入多行字符串或者多个int数值
2021-02-12 11:07:03import java.util.Scanner;public class Main {public static void main(String[] args) {inputStr();inputInteger();inputIntInLine();}//每行输入一个数值,输入多个数值public static void inputInteger() {...import java.util.Scanner;
public class Main {
public static void main(String[] args) {
inputStr();
inputInteger();
inputIntInLine();
}
//每行输入一个数值,输入多个数值
public static void inputInteger() {
Scanner scanner = new Scanner(System.in);
String nextLine = scanner.nextLine();
int sum = 0;
while (nextLine != null && !nextLine.equals("")) {
sum += Integer.parseInt(nextLine);
System.out.println(sum);
nextLine = scanner.nextLine();
}
System.out.println("end of input integer");
}
// 每行输入一个字符串,输入多个字符串
public static void inputStr() {
Scanner scanner = new Scanner(System.in);
String nextLine = scanner.nextLine();
while (nextLine != null && !nextLine.equals("")) {
System.out.println(nextLine);
nextLine = scanner.nextLine();
}
System.out.println("end of input string");
}
//输入多个数值,用空格隔开
public static void inputIntInLine() {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] numstr = str.split(" ");
int[] nums = new int[numstr.length];
for(int i = 0; i < numstr.length; i ++) {
nums[i] = Integer.parseInt(numstr[i]);
}
for(int n: nums) {
System.out.println(n);
}
System.out.println("end of input int in a line");
}
}
做算法题目时用的输入
以下方法在做算法题目时能够正确的结束输入。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//while(sc.hasNextLine()){
while(sc.hasNext()){
String str = sc.nextLine();
System.out.println(str);
}
sc.close();
}
}
-
如何在Java中进行多行输入
2021-07-16 23:23:53I'm trying to take multi-line user input in Java and split the lines into an array, I need this to solve a problem for an online judge. I'm using a Scanner to take input. I cant determine the end of i...I'm trying to take multi-line user input in Java and split the lines into an array, I need this to solve a problem for an online judge. I'm using a Scanner to take input. I cant determine the end of input. I always get an infinite loop, since I don't know the size of input (i.e number of lines)
Terminating input with an empty String (clicking enter) is still an infinite loop. Code provided below.
public static void main(String[] args) {
ArrayList in = new ArrayList();
Scanner s = new Scanner(System.in);
while (s.hasNextLine() == true){
in.add(s.nextLine());
//infinite loop
}
}
I'm not even sure why the loop executes the first time. I believe the hasNextLine() should be false the first time ,since no input was taken yet. Any help or clarification appreciated.
解决方案
You could use the empty line as a loop-breaker:
while (s.hasNextLine()){ //no need for "== true"
String read = s.nextLine();
if(read == null || read.isEmpty()){ //if the line is empty
break; //exit the loop
}
in.add(read);
[...]
}
-
java中从控制台输入多行数据 按回车键输入空行结束
2021-03-13 03:40:11这篇博文是用Windows Live Writer写的,看看效果下面是以前写的程序:java中从控制台输入多行数据 按回车键输入空行结束//试过几个插件,发现插入代码后都不能复制,但是插入图片还是很方便的,用wlw写博客好处是...这篇博文是用Windows Live Writer写的,看看效果
下面是以前写的程序:java中从控制台输入多行数据 按回车键输入空行结束
//试过几个插件,发现插入代码后都不能复制,
但是插入图片还是很方便的,
用wlw写博客好处是可以离线,
以后就用wlw写,然后发布草稿,再到CSDN插入代码后发表文章,只是写个博客要这么艰难吗!!!
Scanner scanner = new Scanner(System.in);
ArrayList ns = new ArrayList<>();
do {
String string = scanner.nextLine();
if (string.equals("")) {
break;
}
ns.add(Integer.valueOf(string));
} while (true);
System.out.println(ns);
输入:
4
5
6
7
输出:
[4, 5, 6, 7]
插入一个图片试试:
插入一个链接试试:
-
如何用java有选择的输入多行文本
2021-02-12 13:39:18java如何有选择的输入多行文本今天在做作业的时候碰到了一个问题:要用java做词频统计,但是这就犯难了,java如何有选择性的进行文件输入输出呢?查阅文档可知,inputStream类和outputStream类是字节流输入输出,而... -
Java控制台如何输入一行、多行?
2021-08-25 22:23:34import java.util.Scanner; 1、输入一个整数 Scanner input=new Scanner(System.in); int a = input.nextInt(); 2、输入一个字符 Scanner input=new Scanner(System.in); char c = input.next()... -
Java Scanner输入多行字符串或者多个int数值
2020-08-24 12:01:491、确定输入行数 // 示例1: // 输入: // 9 // cap // to // cat // card // two // too // up // boat // boot // 输出: // boat // boot // cap // card // -
Java 获取多行输入
2020-08-06 23:03:16数组,已知长度 public class test_0806_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int lent = input.nextInt(); int[] arr = new int[lent];... -
java Scanner输入多行数据后按enter自动结束输入
2021-05-07 23:23:36如:将输入的所有数存入数组并输出 输入: 12 2 43 1 9 87 23 10 28 45 68 输出: [12, 2, 43, 1, 9, 87,23 ,10,28,45,68] public static void saveInput(){ Scanner sc = new Scanner(System.in); //读取... -
java输入多行字符串并计算
2020-04-22 15:22:21要实现输入多行字符串 输入 3+1 3/2 2*4 输出 3+1=4 3/2=1.5 2*4=8 但是现在只能实现  不知道怎么... -
Java如何获取多行字符串输入并判断输入结束
2020-03-06 13:29:07机考的时候,我需要读取多行字符串,而程序的输入是系统自己自动输入,我无法手动多添加一个空格或任何一个我定义的结束符。 现在想想,对于之前说的用ctrl+z我也是没有办法自己输入的,也就是说我只能写好程序,... -
如何在Java中读取多行输入
2021-03-10 01:35:22我们的教授让我们用Java进行一些基本的编程,他提供了一个网站以及所有可以注册和提交问题的内容,因为今天我需要做一个例子,我觉得自己处在正确的轨道上,但是我无法找出其余的。这是实际的问题:**Sample Input:*... -
Java中Scanner的用法:单行/多行输入
2019-07-29 12:12:18Java的Scanner用法,主要用于算法笔试时的控制台输入问题:解决这种情况下的Scanner输入:单行,多行,数值,字符串最好解决的情况单行输入多个字符串多行输入多个字符串 问题:解决这种情况下的Scanner输入:单行,... -
Java程序中如何输入数据
2021-03-13 18:14:27介绍一下我认为最简便实用的一种——Scanner类。...import java.util.Scanner;public class Input {public static void main(String[] args) {Scanner in = new Scanner(System.in);//定义scanne... -
C++实例输入多行数字到数组
2021-01-20 06:24:10C++输入多行数字到数组 前天做某公司笔试题的时候,其输入格式是多行数字,每行以空格为分隔符,以换行符号为结束输入到多个数组。在JAVA中有相应的函数直接将一行拆成数组,感觉在C++中这中输入方式还是挺奇怪的,... -
oj问题:java如何读入多行(行数未知)数据
2019-03-19 16:02:57一般来说,oj网站都会说明自己的输入与输出,一定要去看!!! 比如tsoj,在处理多行数据读入的问题时,帮助如下: import java.util.Scanner; public class Main { //主类必须为 Main,否则会报编译错误 ... -
java scanner 输入多行
2015-12-06 21:09:52Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ ... }这样,不断输入,输入的数据之间可以有空格也可以有换行。所有的数据输入完毕后,按Crtl+Z即可停止输入并退出循环。 -
如何用java实现从键盘的标准多行输入?
2021-03-03 12:38:30import java.io.*;class test//主类{public static void main(String []args)//测试函数{int a;a=MyInput.readInt();System.out.print(a);//System.out.println(a+","+b+","+c);}}class MyInput{public static ... -
Java 的多行字符串 Here Document 的实现
2021-03-09 09:26:49稍好就是用 http://www.htmlescape.net/javaescape_tool.html 把你输入的大段文字生成 Java 的字符串。找过一些介绍 Java 实现 Here Document 的方法,首先大家无一不是把这个多行字符串塞在注... -
JAVA,当键盘输入多行字符串时,如何停止输入。
2014-08-09 08:07:51但是事实上,即使输入了end,按下回车后,依然可以继续输入字符串和回车。 想要知道,如果我需要使这个代码停下来执行下一段代码,在for中应该用什么替换“input[i] != "end"”,以及相应的是如何停止键盘录入的?