如何从用户获取StringBuilder(How to get StringBuilder From user)
我如何通过
Scanner
类从用户那里获得StringBuilder
? 还是其他任何课程? 我知道我们可以得到一个字符串,然后把它放在这样的StringBuilder
:import java.util.Scanner; public class Sade { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str; str = scan.nextLine(); StringBuilder strb = new StringBuilder(str); } }
或者通过使用
append()
。 但无论如何直接从用户获取StringBuilder
?how can i get
StringBuilder
from users throughScanner
class?? or any other classes? i know that we can get a string and then put it in aStringBuilder
like this:import java.util.Scanner; public class Sade { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str; str = scan.nextLine(); StringBuilder strb = new StringBuilder(str); } }
or through using
append()
. but is there anyway to getStringBuilder
directly from user??
原文:https://stackoverflow.com/questions/18087951
最满意答案
问题是你只有一个变量
xmlhttp
,它被声明为一个全局变量。 并且每次调用函数时都通过调用new XMLHttpRequest()
覆盖它。 它创建一个新的,并指定回调,但随后立即通过创建一个新对象来销毁该引用。修复是为每个请求保留一个本地
XMLHttpRequest
对象。function gethtml(URL, elementID){ // using the 'var' keyword makes these variables local in scope - // only accessible from this function if (window.XMLHttpRequest) { var xmlhttp=new XMLHttpRequest(); } else { var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // ... }
使用
var
关键字,它应该按预期工作。The problem is you only have one variable
xmlhttp
, which is declared as a global variable. And you overwrite it by callingnew XMLHttpRequest()
each time the function is called. It creates a new one, and specifies the callbacks, but then immediately goes and destroys that reference by creating a new object.The fix is to keep a local
XMLHttpRequest
object for each request.function gethtml(URL, elementID){ // using the 'var' keyword makes these variables local in scope - // only accessible from this function if (window.XMLHttpRequest) { var xmlhttp=new XMLHttpRequest(); } else { var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } // ... }
Use the
var
keyword and it should work as expected.
相关问答
更多-
使用promises并跳过代码时,Mocha测试会超时,为什么?(Mocha test times out when using promises and skips over code, why?)[2022-08-17]
您的问题是fs.createReadStream是同步的,并返回一个ReadableStream,它以块的形式将数据读入内存。 PromisifyAll将回调转换为异步函数转换为promises,而不是将同步函数转换为异步函数。 可能你想要的是使用fs.readFileAsync : var Promise = require('bluebird'); var fs = Promise.promisifyAll(require('fs')); var path = require('path') var a ... -
为什么async-await比在一起运行它们时的promises慢得多(why is async-await much slower than promises when running them together)[2023-12-22]
当使用按钮Both运行时,您的第一个结果是误导性的。 承诺解决方案在微任务事件队列中排序:因此可以在另一个之前使用console.log进行打印,但是就是为第二个承诺创建第二个承诺和处理其决议。 如果你将runBoth定义为:已经是一个改进: Promise.resolve().then(usingAwait).then(usingPromises) 现在两个承诺都将在微任务中创建,第一个承诺将在第二个承诺创建之前得到解决和处理。 这将导致更公平的比较,其中console.log不在任何时间测量。 You ... -
Promise.join很好,但它可能不适合你的情况。 Promise.all将结合你在那里的几个承诺到一个单一的决议: .then(result => Promise.all([result, departmentDatabase.countUser(departmentID), departmentDatabase.blockedDaysOfResponsible(departmentID)])])) 然后你可以将这个结果(一个数组)分布到一个函数调用中: .spread(function(a, b, ...
-
多次调用异步方法(Calling an async method multiple times)[2022-03-15]
基本上,您要取消先前调用Initialize方法。 在TPL中,如果要取消某些内容,通常应使用CancellationToken 。 你可以这样做的方法是在视图模型中有一个CancellationTokenSource类型的字段,表示取消最后一次调用Initialize 。 当您运行Initialize ,它取消先前的调用,设置自己的取消,调用服务,然后仅在未请求取消时设置属性。 在代码中: class ViewModel { // default value just to avoid a nul ... -
多次调用异步函数(Calling async function multiple times)[2023-08-17]
如果使用fetch和async/await则会容易得多 const fetch = require('node-fetch'); async function PageSpeedCall(website) { const pagespeedCall = `https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=https://${website}&strategy=mobile&key=${keys.pageSpeed}`; ... -
async function function1(i) { return i * i } async function function2(i) { return i * i * i } async function main() { const promises = []; for (let i = 0; i < 10; i++) promises.push(function1(i), function2(i)); ...
-
加载来自同一异步函数的数据,无需承诺多次调用(Load data from same async function calling multiple times without promises)[2021-10-09]
问题是你只有一个变量xmlhttp ,它被声明为一个全局变量。 并且每次调用函数时都通过调用new XMLHttpRequest()覆盖它。 它创建一个新的,并指定回调,但随后立即通过创建一个新对象来销毁该引用。 修复是为每个请求保留一个本地XMLHttpRequest对象。 function gethtml(URL, elementID){ // using the 'var' keyword makes these variables local in scope - // only acces ... -
要严格使用ES6承诺,您需要将每个承诺包装在另一个包装器承诺中,该承诺在包装的承诺得到满足或拒绝时得到解决。 你可以这样做: Promise.all( promises.map( promise => Promise.resolve( promise ).catch( _=>_ ) ) ).then ( function ( ) { // All promises finished } ); 这假设promises是一系列promise和/或值。 To do it strictly usin ...
-
异步流程中的一个大问题是您使用共享变量sourceString来处理结果。 当您对getNewsApi()进行多次调用时,您的结果是不可预测的,并且不会始终相同,因为没有执行异步调用的预定义顺序。 不仅如此,你永远不会重置它,因此所有后续调用也将包括先前调用的结果。 避免在异步调用中修改共享变量,而是直接使用结果。 我已经能够使用下划线的_.after和request-promise使我的应用程序等到所有API调用完成后再调用processWordBank() 尽管可以使用_.after ,但这可以通过pr ...
-
创建'空'或同步。 承诺并不罕见。 甚至还有一种简短的方法: Promise.resolve(value)立即创建并解析Promise。 除此之外,你应该使用适当的链接并停止嵌套事物。 一旦你在一个链中,你甚至不需要解决一个空的promise,因为非thenable对象的返回值被解释为这个。 function scenario(params) { return find(params) .then(data => { let objectExists = checkExistense ...