假设我有一个代码,它要求用户提供一些输入,如下所示:
for (condition) {
System.out.println("Please give some input");
System.in.read();
} //lets say this loop repeats 3 times and i face a problem during second iteration
Run Code Online (Sandbox Code Playgroud)
但我想给用户一个60秒的时间限制,然后抛出异常(在这种情况下,我认为它TimeOutException).我怎么做?
除了Java中的System.out.println()语句外,有人可以通过提供其他打印方式来帮助我吗?
这是我的Java代码:
public class Prog1 {
public static void main(String[] args) {
int x = 5;
while (x > 1) {
x = x + 1;
if (x < 3)
System.out.println("small x");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
small x
Run Code Online (Sandbox Code Playgroud)
我期待一个无限循环...任何想法为什么它这样做?
我有以下代码框架
try {
...
...
sysout("Please provide an input: ");
Thread.sleep(10000); //10 secs time given to the user to give the input. If the user doesn't enter the input it is throwing the exception, which is not being handled (with the custom message)
interact(); //This is a method that belongs to **expectj.Spawn** class
...
...
} catch (Exception e) {
System.out.println("You have not given any input!Please try again!");
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到以下输出 -
Exception in thread "ExpectJ Stream Piper" java.nio.channels.IllegalBlockingModeException
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:39) …Run Code Online (Sandbox Code Playgroud) 我编写了一个包含文本框和按钮的小JSP页面.我试图做的是在我点击按钮后显示我在文本框中写的任何内容(这是一个字符串).它适用于所有场景,但不适用于我将字符串放在双引号("")中的一种情况.我有什么方法可以做到这一点吗?
例如:如果我在文本框中写入 abcd,它可以正常工作.但如果我写 "abcd"(即使用双引号),则双引号内的文本不会显示.
我理解为什么会这样(例如:如果我们在文本框中写abcd,它被视为"abcd",如果我在文本框中写"abcd",它被视为"abcd""),但不能找不到解决方案.
或者根本没有解决方案?
这是我的脚本:
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "Password"
read password
if [ "$password == "pwd" ]; then
echo "Hello"
else
echo "Wrong password"
fi
else
echo "wrong username"
fi
Run Code Online (Sandbox Code Playgroud)
这是我运行时得到的输出:
sh hello.sh Name abcd hello.sh: line 14: unexpected EOF while looking for matching `"' hello.sh: line 16: syntax error: unexpected end of file
这里有什么想法吗?这可能是一个非常愚蠢的,但我浪费了将近一个小时.
这是我的包含setter和getter的类
package Pack;
public class Details {
String FirstName,LastName,City,Country;
public Details(String firstName, String lastName, String city,
String country) {
super();
FirstName = firstName;
LastName = lastName;
City = city;
Country = country;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city; …Run Code Online (Sandbox Code Playgroud) 我有 -
class A {
// contains certain set() and get() methods
}
class B extends A {
public A getAotherMethod() {
A a = new A();
// Contains some logic
return a
}
}
class C extends B {
// contains certain set() and get() methods
}
class D {
public Object getMethod() {
B b = new B();
// Contains some logic
return b.getAnotherMethod()
}
}
public static void main(String[] args) {
A a = new A();
B …Run Code Online (Sandbox Code Playgroud)