我有一个使用while循环逐行读取文件的代码。在while循环中,我有一定条件。有没有一种方法可以根据条件跳过当前行并读取下一行?让我精确地说:
while read Line
do
//some sample conditions
a=$Line
if [ "a" == "b" ]
//i want to go to the next line from this point.
done < **inputfile**
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。
当用户输入"N"或"n"时,我想退出while循环.但它不起作用.它适用于一种情况,但不适用于两种情况.
import java.util.Scanner;
class Realtor {
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
char myChar = 'i';
while(myChar != 'n' || myChar != 'N'){
System.out.println("Do you want see houses today?");
String input = sc.next();
myChar = input.charAt(0);
System.out.println("You entered "+myChar);
}
}
}
Run Code Online (Sandbox Code Playgroud) 嗨这是我的第二个问题.我有下表
|-----|-------|------|------|
|._id.|..INFO.|.DONE.|.LAST.|
|..1..|...A...|...N..|......|
|..2..|...B...|...Y..|..L...|<--- cursor.moveToPosition((int)_id-1);
|..3..|...C...|...Y..|......|
|..4..|...D...|...Y..|......|
|..5..|...E...|...N..|......|
|..6..|...F...|...N..|......|
|-----|-------|------|------|
Run Code Online (Sandbox Code Playgroud)
我用的代码是:
cursor = db.query(TABLE_NAME, new String[]{INFO,DONE,LAST},null,null,null,null,null);
cursor.moveToPosition((int)_id-1);
String Yval = cursor.getString(cursor.getColumnIndex(DONE));
do
{
cursor.moveToNext();
Yval= cursor.getString(cursor.getColumnIndex(DONE));
}
while (Yval=="Y");
s = Yval;
Run Code Online (Sandbox Code Playgroud)
我最初将光标指向我访问的最后一行,然后我创建一个循环来遍历DONE列中的值,而不是在列的行中有Y的情况下停止.当N出现在循环中时,循环应该停止.但它不起作用.Yval永远不等于"Y".因此,光标会执行一个'moveToNext',然后退出循环,因为它不会将Yval读为"Y".(我也将所有内容都更改为整数.1表示N,0表示Y,但仍然无效)所以我在这里做错了什么?
从未见过这样的事情.简单的循环:
t_end = 100.0
t_step= 0.1
time = 0
while time<=t_end:
time+=t_step
print time
Run Code Online (Sandbox Code Playgroud)
最后3个印刷值:
...
99.9
100.0
100.1
Run Code Online (Sandbox Code Playgroud)
看起来对我来说.
现在,我将t_step更改为0.01:
t_end = 100.0
t_step= 0.01
time = 0
while time<=t_end:
time+=t_step
print time
Run Code Online (Sandbox Code Playgroud)
最后3个印刷值:
...
99.98
99.99
100.0
Run Code Online (Sandbox Code Playgroud)
问题:为什么在time = t_end = 100.0时它不会进入最后一个循环?
什么是替代解决方案?
我是Java我可以写这个:
Matcher m;
while ((m = pattern.matcher(string)).matches()) {...}
Run Code Online (Sandbox Code Playgroud)
我如何在Scala中执行此操作?这不起作用:
var m: Matcher = null
while ((m = pattern.matcher(s)).matches()) {}
Run Code Online (Sandbox Code Playgroud) 任何人都可以解释为什么以下无限循环需要大量的CPU使用(CPU usage has increased upto 50% if total core is 2 and 100% if total CPU core is just 1)
但是,如果我取消注释该行,它会减少到1或2个CPU使用率?
public class Program
{
public static void Main(string[] args)
{
while (true)
{
//Console.WriteLine("hello..");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我工作在Java中的一个项目,是一种像数字时间机器,它基本上保存在什么目录发生在一个特定的沙爹,其档案到一个特定的文件,并将其存储的地方保管.现在我正在研究密码功能.我有基础知识来检查它是对还是错,但当我输入错误答案时,课程在说"无效密码"后结束.有没有办法让它从else语句的结尾循环到开头,main所以我不必每次都重新执行该类?
import java.util.Scanner;
import java.util.*;
public class Adam {
public static void main(String args[]) {
String passwrd = "password";
String inputPassword;
System.out.print("Please Input Password ");
Scanner sc = new Scanner(System.in);
inputPassword = sc.nextLine();
if (inputPassword.equals(passwrd)) {
System.out.println("Password Accepted, Loading Archive...");
} else {
System.out.println("Invalid Password");
}
}
}
Run Code Online (Sandbox Code Playgroud) int findMax(int*sums){
int t = 0;
int max = sums[0];
while (sums[t] != '\0'){
printf("current max: %d %d\n", max, sums[t]);
if (sums[t] > max ){
max = sums[t];
}
t ++;
}
return max;
}
Run Code Online (Sandbox Code Playgroud)
这输出:
current max: 7 7
current max: 7 4
current max: 7 2
Run Code Online (Sandbox Code Playgroud)
它忽略了列表的其余部分sums.我认为这是因为下一个元素sums是0.但我不明白为什么它会被0视为'\0'(null).
我有以下Kotlin功能:
fun func(n: Int): Int {
var count = 1
var m = n
while(m != 1) {
m = if(m.isOdd()) 3 * m + 1 else m / 2
count++
}
return count
}
Run Code Online (Sandbox Code Playgroud)
我想用"功能"风格编写这个简单的算法,使用Kotlin的运算符,如map(),count()等.我能想出的最接近的是:
fun func(n: Int): Int {
return n.toList()
.map{ if(it.isOdd()) 3*it+1 else it/2 }
.takeWhile { it != 1 }
.count()
}
Run Code Online (Sandbox Code Playgroud)
显然,上面的代码不起作用,因为map只执行一次,但你知道我想要实现的目标.
PS:toList()只是一个扩展函数,它将int转换为包含该int的列表:
fun Int.toList() = listOf(this)
Run Code Online (Sandbox Code Playgroud) 我被困在那.所以我在我的代码中有这个while-read循环需要很长时间,我想在很多处理器中运行它.但是,我想分割输入文件并运行14个循环(因为我有14个线程),每个分割文件一个并行运行.事情是,我不知道如何告诉while循环哪个文件可以使用和使用.
例如,在常规的while-read循环中,我会编码:
while read line
do
<some code>
done < input file or variable...
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我想将上面的输入文件拆分为14个文件,并在并行循环中运行14个循环,每个拆分文件一个循环.我试过了 :
split -n 14 input_file
find . -name "xa*" | \
parallel -j 14 | \
while read line
do
<lot of stuff>
done
Run Code Online (Sandbox Code Playgroud)
也试过了
split -n 14 input_file
function loop {
while read line
do
<lot of stuff>
done
}
export -f loop
parallel -j 14 ::: loop
Run Code Online (Sandbox Code Playgroud)
但是我都没能告诉哪个文件是循环的输入,所以并行会理解"将每个xa*文件并行放入各个循环"
输入文件的示例(字符串列表)
AEYS01000010.10484.12283
CVJT01000011.50.2173
KF625180.1.1799
KT949922.1.1791
LOBZ01000025.54942.57580
Run Code Online (Sandbox Code Playgroud)
编辑
这是代码.输出是一个表(741100行),其中包含有关已经进行的DNA序列比对的一些统计数据.循环采用input_file(没有折断线,从500到~45000行,800Kb不等)进行DNA序列分析,逐行读取并查找每个对应的数据库中的完整分类(~45000行) .然后,它做了一些总和/分裂.输出是.tsv,看起来像这样(序列"KF625180.1.1799"的示例):
Rate of taxonomies for this sequence …Run Code Online (Sandbox Code Playgroud)