String s = num + "";
时间: 2024-05-27 17:14:43 浏览: 11
This code is incomplete and will result in a compilation error.
Assuming that "num" is a variable containing a numerical value, the correct code to convert it to a string would be:
String s = String.valueOf(num);
or
String s = Integer.toString(num);
相关问题
帮我排错 #include <iostream> #include <stack> #include <cstring> using namespace std; int n; int solve(string s) { stack<int> st; char sign = '+'; int num = 0; int ans = 0; for(int i = 0; i < s.size(); i++) { if(s[i] == ' ') { continue; } if(s[i] >= '0' && s[i] <= '9') { num = num*10+(s[i]-'0'); } if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || i == s.size()-1) { if(sign == '+') { st.push(num); } if(sign == '-') { st.push(-num); } if(sign == '*') { st.top() *= num; } if(sign == '/') { st.top() *= num; } sign = s[i]; num = 0; } } while (!st.empty()) { ans += st.top(); st.pop(); } return ans; } int main() { string s; getline(cin, s); cout << solve(s) << endl; return 0; }
在计算除法时,代码中写成了 `st.top() *= num`,实际应该是 `st.top() /= num`。
修改后的代码如下所示:
```cpp
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int n;
int solve(string s) {
stack<int> st;
char sign = '+';
int num = 0;
int ans = 0;
for(int i = 0; i < s.size(); i++) {
if(s[i] == ' ') {
continue;
}
if(s[i] >= '0' && s[i] <= '9') {
num = num*10+(s[i]-'0');
}
if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || i == s.size()-1) {
if(sign == '+') {
st.push(num);
}
if(sign == '-') {
st.push(-num);
}
if(sign == '*') {
st.top() *= num;
}
if(sign == '/') {
st.top() /= num; // 修改这一行
}
sign = s[i];
num = 0;
}
}
while (!st.empty()) {
ans += st.top();
st.pop();
}
return ans;
}
int main() {
string s;
getline(cin, s);
cout << solve(s) << endl;
return 0;
}
```
将以下js代码转 java代码function $c(ascii){ return String.fromCharCode(ascii); } function dhc_cacheEncrypt(x){ var constval=37,out=""; x = x.replace(/(\s*$)/g,""); var len ; var ch,num; for(var i=1; i<= x.length; i++){ ch = x.charAt(i-1) if(ch=="D") ch = $c(2); if(ch=="p") ch = $c(3); if(ch=="d") ch = $c(4); if(ch=="t") ch = $c(5); num = ch.charCodeAt(); num = (num-i+constval)%255; if (num>127) num = (num+128)%255; if (num<32) num = (num+40)%255; if ($c(num)=="^") num++; out += ""+$c(num%255); } len = out.length; for(var i=len; i<12; i++){ ch = out.charAt(i-len); num = ch.charCodeAt(); num = (num*2.345*constval*(constval-7))%255; if (num>127) num = (num+128)%255; if (num<32) num = (num+40)%255; if ($c(num)=="^") num++; out += ""+$c(num%255); } return out; }
以下是将js代码转换为java代码后的样子:
public String $c(int ascii) {
return Character.toString((char)ascii);
}
public String dhc_cacheEncrypt(String x) {
int constval = 37;
String out = "";
x = x.replaceAll("(\\s*$)", "");
int len;
char ch;
int num;
for(int i=1; i<=x.length(); i++) {
ch = x.charAt(i-1);
if(ch=='D') ch = $c(2).charAt(0);
if(ch=='p') ch = $c(3).charAt(0);
if(ch=='d') ch = $c(4).charAt(0);
if(ch=='t') ch = $c(5).charAt(0);
num = (int)ch;
num = (num-i+constval)%255;
if(num>127) num = (num+128)%255;
if(num<32) num = (num+40)%255;
if($c(num).equals("^")) num++;
out += "" + $c(num%255);
}
len = out.length();
for(int i=len; i<12; i++) {
ch = out.charAt(i-len);
num = (int)ch;
num = (int)((num*2.345*constval*(constval-7))%255);
if(num>127) num = (num+128)%255;
if(num<32) num = (num+40)%255;
if($c(num).equals("^")) num++;
out += "" + $c(num%255);
}
return out;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)