比较整数和字符串的Java代码

时间:2017-10-12 15:37:34

标签: java string int

我一直在努力为我正在研究的项目做这项工作,无论我尝试int和字符串不会比较,当我只是一个整数它不能正常工作。有没有办法让代码工作并比较整数和字符串?

if( a == Lust || b != Lust) {
   win = win + 20;
   balance = balance - 20;
}

1 个答案:

答案 0 :(得分:1)

您想要将int与String进行比较,因此我假设String中有一个数字。

<强>爪哇

SELECT UserID, SUM(Campaign_Interactions) AS Interactions, SUM(Transactions) AS Transactions, ROUND(SUM(Transactions)/SUM(Campaign_Interactions), 2) AS Con_Score, MasterChannel FROM(
(SELECT customDimension.value AS UserID, visitid AS visitid1, trafficSource.campaign AS Campaign, COUNT(trafficSource.campaign) AS Campaign_Interactions, SUM (totals.transactions) AS Transactions, ROUND(MAX(totals.transactions)/COUNT(trafficSource.campaign), 2) AS Conversion_Score
FROM `xxx.ga_sessions_20*` AS m
  CROSS JOIN UNNEST(m.customdimensions) AS customDimension
  CROSS JOIN UNNEST (hits) AS hit
WHERE parse_date('%y%m%d', _table_suffix) between 
DATE_sub(current_date(), interval 7 day) and
DATE_sub(current_date(), interval 1 day)
AND customDimension.index = 2
AND trafficSource.campaign IS NOT NULL
AND (customDimension.value NOT LIKE 'true' AND customDimension.value NOT LIKE 'undefined')
AND hit.isentrance IS TRUE
GROUP BY visitid1, Campaign, userID
ORDER BY Transactions DESC)

JOIN

(SELECT * FROM `xxx.7Days_VisitID_MasterChan`)
ON visitid1 = visitid)
GROUP BY UserID, MasterChannel
ORDER BY UserID

您可以使用另一个解决方案String s = "123"; int a = 123; int b = Integer.parseInt(s); if(a == b) { //true } else { //false } 并将两个变量比较为字符串(使用String.valueOf(a)),但是将字符串解析为int需要更长的时间。 (Java: Comparing ints and Strings - Performance

<强>的Javascript

s.equals(String.valueOf(a))